-3

I want to sort the array by status_id using PHP functions. How can I go about this?

Here is a sample of the array:

Array
(
    [0] => stdClass Object
        (
            [status_id] => 4
            [status_content] => {"text":"DET VIRKER JO IKKE!!!! ### >"}
            [status_likes] => 
            [status_dislikes] => 
            [status_owner] => 20
        )

    [1] => stdClass Object
        (
            [status_id] => 2
            [status_content] => {"yt_vid":"Hb3MWuZT_W0","text":"Det her er jo bare awesome!!!"}
            [status_likes] => 
            [status_dislikes] => 
            [status_owner] => 20
        )

    [2] => stdClass Object
        (
            [status_id] => 8
            [status_content] => {"text":"Hej"}
            [status_likes] => 
            [status_dislikes] => 
            [status_owner] => 1
        )

    [3] => stdClass Object
        (
            [status_id] => 7
            [status_content] => {"text":"ff"}
            [status_likes] => 
            [status_dislikes] => 
            [status_owner] => 1
        )

    [4] => stdClass Object
        (
            [status_id] => 6
            [status_content] => {"text":"h"}
            [status_likes] => 
            [status_dislikes] => 
            [status_owner] => 1
        )

    [5] => stdClass Object
        (
            [status_id] => 5
            [status_content] => {"text":""}
            [status_likes] => 
            [status_dislikes] => 
            [status_owner] => 1
        )

    [6] => stdClass Object
        (
            [status_id] => 3
            [status_content] => {"yt_vid":"PSLDZVh1Vtw","text":"This is really funny"}
            [status_likes] => 
            [status_dislikes] => 
            [status_owner] => 1
        )

    [7] => stdClass Object
        (
            [status_id] => 1
            [status_content] => {"text":"This works, great! huh?"}
            [status_likes] => 
            [status_dislikes] => 
            [status_owner] => 1
        )

)
Chris Forrence
  • 10,042
  • 11
  • 48
  • 64
AwesomeGuy
  • 573
  • 3
  • 14
  • You will have to read beyond the top answer on the previous question to get information that specifically applies to your problem, as the top answer appears to be "Allow me to demonstrate the implementation of several sorting algorithms in PHP". – TML Oct 23 '14 at 12:18

3 Answers3

0

You need to use array_multisort

http://php.net/manual/en/function.array-multisort.php

First loop though to get a simple array of all the items toy wish to sort on (status id) and then use that variable with the array you wantto sort in array_multisort

exussum
  • 18,275
  • 8
  • 32
  • 65
0

One option is to use usort with a custom sorting function. Here's a little function that will build a closure for you based on some property name:

function buildSorter($key) {
    return function ($a, $b) use ($key) { return strnatcmp($a->$key, $b->$key); };
}

Using this, you could:

usort($data, buildSorter('status_content'));

or

usort($data, buildSorter('status_owner'));

with equal ease.

TML
  • 12,813
  • 3
  • 38
  • 45
0

Thank you for the help! I know finally found the answer by looking a little better around.

Take a look.

usort($r, function($a, $b) { return $a['status_id'] - $b['status_id']; });

AwesomeGuy
  • 573
  • 3
  • 14