1

I have an array for example

Array
(
    [0] => Array
        (
            [id] => 6
            [name] => ah
            [order] => 4
        )
    [1] => Array
        (
            [id] => 5
            [name] => hz
            [order] => 
        )
    [2] => Array
        (
            [id] => 7
            [name] => ch
            [order] => 
        )

    [3] => Array
        (
            [id] => 5
            [name] => 
            [order] => 
        )

    [4] => Array
        (
            [id] => 4
            [name] => zh
            [order] => 1
        )
)

It needs to be sorted first by "order", if order isn't available, it is sorted in alphabetical order with the "name" (but those arrays goes after all the arrays that have the sort order), and if no "order" and no "name", it goes at the end of the array.

So the above array would need to become:

Array
(
    [0] => Array
        (
            [id] => 4
            [name] => zh
            [order] => 1
        )
    [1] => Array
        (
            [id] => 6
            [name] => ah
            [order] => 4
        )
    [2] => Array
        (
            [id] => 7
            [name] => ch
            [order] => 
        )
    [3] => Array
        (
            [id] => 5
            [name] => hz
            [order] => 
        )
    [4] => Array
        (
            [id] => 5
            [name] => 
            [order] => 
        )

)

I've tried some for looping but nothing close to a solution.

2 Answers 2

4

Have you looked at array_multisort?

foreach ($data as $key => $row) {
   $name[$key]  = $row['name'];
   $order[$key] = $row['order'];
}

array_multisort($name, SORT_DESC, $order, SORT_ASC, $data);

PHP array multisort

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

3 Comments

Checking array multisort. Ps this code doesn't return the correctly sorted array.
@robertparé the code was simply an example of using array_multisort. By combining loops with sorts your problem should be solved. array_multisort($order, SORT_ASC, $name, SORT_ASC, $data);
Could you give the answer so the resulted array is correct? I have trouble with looping and can't find the solution with array multisort either.
1

array_multisort() might not provide enough control to achieve the sorting you require.

As an alternative, you could use uasort() and provide a custom comparison function.

For example:

function compare($a, $b) {
    if (empty($a['order']) && !empty($b['order'])) {
        return 1;
    }

    if (!empty($a['order']) && empty($b['order'])) {
        return -1;
    }

    if ($a['order'] == $b['order']) {
        return strnatcmp($a['name'], $b['name']);
    }

    return ($a['order'] < $b['order']) ? -1 : 1;
}

uasort($arr, 'compare');

1 Comment

Working great, thanks. I just added strtolower() to both $a['name'] and $b['name'] else it wasn't sorting properly

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.