php - Sort array and child arrays by value -


i have array like:

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

i need sort outer arrays (2 & 4) key 'position', ascending (0 upwards), , sort each inner array of ('children'), respective position.

there might 6 main arrays, 6 'children' arrays sort.

what best way this?

if understand explanation of problem correctly, following code work you:

//sort outer array usort($array, function($a, $b) {     return $a['position'] - $b['position']; }); //sort childrens foreach ($array &$item) {     usort($item['children'], function($a, $b) {         return $a['position'] - $b['position'];     }); } 

in case, usort native php function handy described case. http://php.net/manual/en/function.usort.php


Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -