1

I've got a bunch of content pulled out of several databases and compiled into a nested array like this:

Array
(
    [0] => Array
        (
            [id] => 3
            [published] => 1433940002
            [content] => This is some content
        )
     [1] => Array
        (
            [id] => 52
            [published] => 1433940001
            [content] => This is some more content
        )
     [2] => Array
        (
            [id] => 16
            [published] => 1433940003
            [content] => This is some more content
        )
)

Since I cannot sort the content whilst it is retrieved (because it is done using several queries from as many databases) I want to dig down a level into the array and sort by the "published" date whilst maintaining the depth of the array so I end up with...

Array
(
    [0] => Array
        (
            [id] => 52
            [published] => 1433940001
            [content] => This is some more content
        )
     [1] => Array
        (
            [id] => 3
            [published] => 1433940002
            [content] => This is some content
        )
     [2] => Array
        (
            [id] => 16
            [published] => 1433940003
            [content] => This is some more content
        )
)

I'm pretty sure array_multisort() is the way to go but I can't think where to start! Could somebody be so kind as to give me a pointer?

Doug
  • 823
  • 2
  • 10
  • 20

1 Answers1

0

Thanks to @Rizier123's policing of my question I figured it out myself. For anyone else finding this thread here's my solution using the example array I supplied above.

//-- get disorganised content from databases here

usort($array, "publishcmp");

//-- output organised content here

function publishcmp($a , $b){
    if ($a['published'] == $b['published']) {
        return 0;
    }
    return ($a['published'] < $b['published']) ? -1 : 1;
}

Pretty much taken verbatim from php.net

Doug
  • 823
  • 2
  • 10
  • 20