这是我的简单代码。
<?php
$num=array('a'=>5, 'b'=>3, 'c'=>1);
?>如何在不使用asort()的情况下维护索引关联的同时对此数组进行排序?
发布于 2017-03-05 18:46:00
检查Custom_asort()示例
<?php
/* Custom asort function */
function custom_asort($num)
{
$return = $numbers = array(); // empty array
foreach($num as $key => $val)
{
$numbers[] = $val ;
}
sort($numbers); // sort number
$arrlength = count($numbers); // count array lenght
for($x = 0; $x < $arrlength; $x++) {
$key = array_search($numbers[$x], $num); // array_search apply for search key according to index
$return[$key] = $numbers[$x];
}
echo "<pre>";
print_r($return); // print function output
}
$num=array('a'=>5, 'b'=>3, 'c'=>1, 'd'=>2); // array
custom_asort($num); // calling function
?>https://stackoverflow.com/questions/42607171
复制相似问题