Created
May 31, 2016 07:18
-
-
Save rajvanshipradeep15/5102cf1c21249fd049e8dfeb2ca669d3 to your computer and use it in GitHub Desktop.
sort multidimentional array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// To sort multidimentional array | |
public 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; | |
} | |
Example: | |
$this->array_msort($arrAllFundAllocation['growth_equity'], array('fund_id'=>SORT_ASC)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment