php - USort Multi Index Array - Doesn't Return Correctly -
i'm having trouble phps usort()
function. have multi-indexed array looks this:
array ( [0] => array ( [0] => array ( [title] => c [more_fields] => '...' ) [1] => array ( [title] => [more_fields] => '...' ) [2] => array ( [title] => b [more_fields] => '...' ) [3] => array ( [title] => xz [more_fields] => '...' ) ) )
i'm trying loop through array , sort each index title
index. here's code without of test inputs:
foreach( $my_arr $arr ) { if( count( $arr ) > 1 ) { usort( $arr, function( $a, $b ) { return strcasecmp( $a['title'], $b['title'] ); } ); } }
now expect work, , know i'm hitting right indexes, if echo out $a['title']
, $b['title']
before return data looks like:
a _ c | xz _ | b _ | c _ | b _ c | xz _ b | c _ b | xz _ c
that's fine , dandy, once foreach
finished, array unchanged. looks like, looking @ php docs usort()
passing array reference , returning booleans on completion. missing in usort()
keeps leaving array unchanged?
foreach
works on copy of array. modify actual array need reference value (notice &
):
foreach( $my_arr &$arr ) {
or should able use key , actual array $my_arr[$key]
:
foreach( $my_arr $key => $arr ) { if( count( $arr ) > 1 ) { usort( $my_arr[$key], function( $a, $b ) { return strcasecmp( $a['title'], $b['title'] ); } ); } }
Comments
Post a Comment