Skip to content

Instantly share code, notes, and snippets.

@rajvanshipradeep15
Created May 31, 2016 07:18
Show Gist options
  • Save rajvanshipradeep15/5102cf1c21249fd049e8dfeb2ca669d3 to your computer and use it in GitHub Desktop.
Save rajvanshipradeep15/5102cf1c21249fd049e8dfeb2ca669d3 to your computer and use it in GitHub Desktop.
sort multidimentional array
// 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