开发者

Sort multi-dimensional array BUT do not reassign top-level keys

开发者 https://www.devze.com 2023-03-16 19:57 出处:网络
array $tagHolder[$row[\'id\']] = array( \"name\" => $row[\'name\'], \"primary\" =>开发者_如何学编程; $row[\'primary\'],

array

$tagHolder[$row['id']] = array(


        "name" => $row['name'],
        "primary" =>开发者_如何学编程; $row['primary'],
        "child" => $row['child'],
        "order" => $row['order']

    );

usort function

function sortAsc($x, $y){
if ( $x['order'] == $y['order'] )
 return 0;
else if ( $x['order'] < $y['order'] )
 return -1;
else
 return 1;
}

Will order by 'order' BUT will not keep the original $row['id'] keys, instead it reassigns the first prosition as 0 and so on. How can I make the sort function sort but keep the $row['key'] untouched?


Use uasort instead of usort to keep the key association.

0

精彩评论

暂无评论...
验证码 换一张
取 消