3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php function array_msort($array, $cols) { $colarr = array(); foreach ($cols as $col => $order) { $colarr[$col] = array(); foreach ($array as $k => $row) { $colarr[$col]['_'.$k] = strtolower($row[$col]); } } $eval = 'array_multisort('; foreach ($cols as $col => $order) { $eval .= '$colarr[\''.$col.'\'],'.$order.','; } $eval = substr($eval,0,-1).');'; eval($eval); $ret = array(); foreach ($colarr as $col => $arr) { foreach ($arr as $k => $v) { $k = substr($k,1); if (!isset($ret[$k])) $ret[$k] = $array[$k]; $ret[$k][$col] = $array[$k][$col]; } } return $ret; } $a = array('a'=>array('aa'=>'3','bb'=>'c'),'b'=>array('aa'=>'1','bb'=>'a'),'c'=>array('aa'=>'2','bb'=>'b')); $b = array_msort($a, array('aa'=>SORT_ASC)); var_dump($a); var_dump($b); ?>
based on 6u7Cv
Output for 4.3.0 - 7.1.0
array(3) { ["a"]=> array(2) { ["aa"]=> string(1) "3" ["bb"]=> string(1) "c" } ["b"]=> array(2) { ["aa"]=> string(1) "1" ["bb"]=> string(1) "a" } ["c"]=> array(2) { ["aa"]=> string(1) "2" ["bb"]=> string(1) "b" } } array(3) { ["b"]=> array(2) { ["aa"]=> string(1) "1" ["bb"]=> string(1) "a" } ["c"]=> array(2) { ["aa"]=> string(1) "2" ["bb"]=> string(1) "b" } ["a"]=> array(2) { ["aa"]=> string(1) "3" ["bb"]=> string(1) "c" } }