0

I have an array as below:

Array
(
    [0] => Array
        (
            [id] => 2
            [type] => comment
            [batch] => Array
                    (
                        [0] => Array
                            (
                                [id] => T001
                                [datetime] => 2010-05-15 11:29:00
                            )
                        [1] => Array
                            (
                                [id] => T003
                                [datetime] => 2010-05-15 11:33:00
                            )
                    )
        )

    [1] => Array
        (
            [id] => 3
            [type] => status
            [batch] => Array
                    (
                        [0] => Array
                            (
                                [id] => T002
                                [datetime] => 2010-05-15 11:31:00
                            )
                        [1] => Array
                            (
                                [id] => T004
                                [datetime] => 2010-05-15 12:32:00
                            )
                        [2] => Array
                            (
                                [id] => T006
                                [datetime] => 2010-05-15 12:33:00
                            )
                    )
        )

    [2] => Array
        (
            [id] => 4
            [type] => status
            [batch] => Array
                    (
                        [0] => Array
                            (
                                [id] => T005
                                [datetime] => 2010-05-15 12:34:00
                            )
                        [1] => Array
                            (
                                [id] => T007
                                [datetime] => 2010-05-15 13:35:00
                            )
                        [2] => Array
                            (
                                [id] => T008
                                [datetime] => 2010-05-15 10:36:00
                            )
                    )
        )
)

I would like to re-order the array by only the last datetime inside the sub array [batch]

the result should be like this:

Array
(
    [0] => Array
        (
            [id] => 4
            [type] => status
            [batch] => Array
                    (
                        [0] => Array
                            (
                                [id] => T005
                                [datetime] => 2010-05-15 12:34:00
                            )
                        [1] => Array
                            (
                                [id] => T007
                                [datetime] => 2010-05-15 13:35:00
                            )
                        [2] => Array
                            (
                                [id] => T008
                                [datetime] => 2010-05-15 10:36:00
                            )
                    )
        )

    [1] => Array
        (
            [id] => 2
            [type] => comment
            [batch] => Array
                    (
                        [0] => Array
                            (
                                [id] => T001
                                [datetime] => 2010-05-15 11:29:00
                            )
                        [1] => Array
                            (
                                [id] => T003
                                [datetime] => 2010-05-15 11:33:00
                            )
                    )
        )

    [2] => Array
        (
            [id] => 3
            [type] => status
            [batch] => Array
                    (
                        [0] => Array
                            (
                                [id] => T002
                                [datetime] => 2010-05-15 11:31:00
                            )
                        [1] => Array
                            (
                                [id] => T004
                                [datetime] => 2010-05-15 12:32:00
                            )
                        [2] => Array
                            (
                                [id] => T006
                                [datetime] => 2010-05-15 12:33:00
                            )
                    )
        )
)

Can I do it with "array_multisort"? Please suggest. Thank you very much!

1 Answer 1

1

Use usort which allows you to sort an array using a user-defined comparison function. (PHP Docs)

usort($array, function($a, $b) {
    $lastTimestampA = end($a['batch']);
    $lastTimestampB = end($b['batch']);
    return strtotime($lastTimestampA['datetime']) > strtotime($lastTimestampB['datetime']);
})
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! ~~~~~~~~ I've this error ==> usort(): Array was modified by the user comparison function ~~~~~~ If I do it @usort( .... ), the array wasn't sort.

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.