-1

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.

1
  • This question is not a good fit for Stack Overflow because it does not contain a minimal reproducible example. Commented Jan 22, 2024 at 0:57

2 Answers 2

2
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)

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.