user6606700 user6606700 - 4 months ago 6
PHP Question

associative array in sort using php

$array = array('0'=>'5', '1'=>'4', '2'=>'1', '3'=>'2');

Array
(
[0] => 5
[1] => 4
[2] => 1
[3] => 2
)


Expecting result

Array
(
[0] => 1
[1] => 2
[2] => 4
[3] => 5
)

$array = array('0'=>'5', '1'=>'4', '2'=>'1', '3'=>'2');

$results = [];

foreach($array as $key=>$value){
$results[$key] = arsort($value);
}

echo "<pre>";
print_r($results);


Please suggest how do we can sort associative array i did try but does not work for me please guide

Answer

As per your "expected results' it seems like you don't wish to maintain the keys. If that's the case then you can just use sort.

Something like this..

$array = array('0'=>'5', '1'=>'4', '2'=>'1', '3'=>'2');
sort($array);
print_r($array);
Comments