Reputation: 9
I want to sort multidimensional array values from not null to null. I want to sort my multidimensional array if the address fields like street, post numbers are null then they should display in last page.
Upvotes: -1
Views: 420
Reputation: 722
function array_sort($array, $on, $order=SORT_ASC) {
$new_array = array();
$sortable_array = array();
if (count($array) > 0) {
foreach ($array as $k => $v) {
if (is_array($v)) {
foreach ($v as $k2 => $v2) {
if ($k2 == $on) {
$sortable_array[$k] = $v2;
}
}
} else {
$sortable_array[$k] = $v;
}
}
switch ($order) {
case SORT_ASC:
asort($sortable_array);
break;
case SORT_DESC:
arsort($sortable_array);
break;
}
foreach ($sortable_array as $k => $v) {
$new_array[$k] = $array[$k];
}
}
return $new_array;
}
something along the lines of that works for me (more or less copy pasted from http://php.net/manual/en/function.sort.php)
Upvotes: 2
Reputation: 3402
I'm not sure about PHP but in general I would say you need to slice your array so to divide the null
set from the not null
set, then sort the not null
set based on your criteria and then merge it with the null
set.
Upvotes: 0