0

I need to sort my array php by a key value. my array:

Array
(
    [1430039342393636453] => Array
        (
            [0] => Array
                (
                    [thrid] => 1430039342393636453
                    [uid] => 19748
                    [flag] => 1
                    [timestamp] => 1363791789
                    [date] => Mar 20
                    [content_preview] => 
                    [content] => 

                )

        )
[1430750471744336569] => Array
        (
            [0] => Array
                (
                    [thrid] => 1430750471744336569
                    [uid] => 19870
                    [flag] => 1
                    [timestamp] => 1364469959
                    [date] => Mar 28
                    [content_preview] =>
                    [content] =>
                )

            [1] => Array
                (
                    [thrid] => 1430750471744336569
                    [uid] => 19874
                    [flag] => 1
                    [timestamp] => 1364472417
                    [date] => Mar 28
                    [content_preview] => 
                    [content] => 
                )
)

I need to sort by timestamp the main array and also the childs arrays. Any suggesitons?

3
  • Have you tried to look up "sort php". php.net/manual/en/array.sorting.php Commented Apr 4, 2013 at 15:00
  • I would suggest doing research first before asking on SO. Commented Apr 4, 2013 at 15:01
  • RTLM: php.net/usort Commented Apr 4, 2013 at 15:03

2 Answers 2

1

Use asort to sort associative arrays.

Related S.O. Post:

Sorting an associative array in PHP

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

Comments

0

Try something like:

private function sort($sort) {

    foreach($sort as &$arr) { //use a reference because usort uses references to manipulate your array.
                            //if you don't pass by reference you'll never see the sort
        usort($arr, function($a, $b) { return $a['timestamp'] - $b['timestamp'];
    }

    usort($sort, function($a, $b) { return key($a) - key($b); }

    return $sort;

}

5 Comments

Thx, but this is wrong usort($sort, function($a, $b) { return key($b) - key($a); }); - I need to sort $sort by timestamp not by key!
You said you needed to sort by timestamp in the "main array" and "child arrays" which I took to mean the array containing the 2 child-arrays as being the "main" and the two children as "child" where the "main" array has only 2 elements, each arrays, with timestamps as their keys. I assumed you wanted the parent sorted by the timestamp key and the children by the timestamp value. The code above will sort your array. It's up to you to adapt generic and/or pseudo code from S.O. to meet your specific need. usort().
Actually, if you want to keep your keys, you'll need to use asort() as @Jordan suggested. I'll update my example.
Actually, you should use usort(), you'll just need to modify your array format to protect your keys.
You'll want to put anything with an index you want to protect in a sub array, so that PHP changes the index of the new parrent and your old index is left alone and accessable..

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.