psot
psot

Reputation: 194

Sorting objects in array by highest value

I have a foreach loop which finds comment replies and stores them in $child_comments

After

<?php  echo '<pre>';
print_r($child_comments);
echo '</pre>'; ?>

I get a separate array for each of the parent comments:

Array
(
    [0] => stdClass Object
        (
            [comment_ID] => 603
            [comment_parent] => 600
            [user_id] => 2
        )

    [1] => stdClass Object
        (
            [comment_ID] => 601
            [comment_parent] => 600
            [user_id] => 2
        )

)

Array
(
     [0] => stdClass Object
        (
            [comment_ID] => 584
            [comment_parent] => 580
            [user_id] => 1
        )
)

Array
(
    [0] => stdClass Object
        (
            [comment_ID] => 608
            [comment_parent] => 520
            [user_id] => 2
        )

    [1] => stdClass Object
        (
            [comment_ID] => 598
            [comment_parent] => 520
            [user_id] => 2
        )

    [2] => stdClass Object
        (
            [comment_ID] => 521
            [comment_parent] => 520
            [user_id] => 2
        )

)

But I need to sort and output the comments by their comment id, from highest ID to lowest ID.

I can get the comments I like with

foreach ($child_comments as $objects) {
  echo $objects->comment_ID;
}

but still they will be sorted by their parent comments. Any ideas? The ideal structure would be something like this:

Array
(
    [0] => stdClass Object
        (
            [comment_ID] => 608
            [comment_parent] => 520
            [user_id] => 2
        )
    [1] => stdClass Object
        (
            [comment_ID] => 603
            [comment_parent] => 600
            [user_id] => 2
        )

    [2] => stdClass Object
        (
            [comment_ID] => 601
            [comment_parent] => 600
            [user_id] => 2
        )

    [3] => stdClass Object
        (
            [comment_ID] => 598
            [comment_parent] => 520
            [user_id] => 2
        )

    [4] => stdClass Object
        (
            [comment_ID] => 584
            [comment_parent] => 580
            [user_id] => 1
        )

    [5] => stdClass Object
        (
            [comment_ID] => 521
            [comment_parent] => 520
            [user_id] => 2
        )
)

Upvotes: 1

Views: 607

Answers (2)

Scopey
Scopey

Reputation: 6319

If you have a complex sorting criteria the best way to do it is with a call back using usort.

For example:

usort($arrayOfObjects, function($a, $b){
    if($a->comment_parent > $b->comment_parent) return 1;
    elseif($a->comment_parent < $b->comment_parent) return -1;
    else{
        // Do child compare here...
    }
});

Upvotes: 0

Rohan Kumar
Rohan Kumar

Reputation: 40639

If you get every comments in different arrays then first you can use array_merge() to create a single array, then use usort() to sort your final array like,

<?php
    $comments=array_merge($child_comments);
    function cmp($a, $b) {
        return $b['comment_ID'] - $a['comment_ID'];//use $b->comment_ID if Object
    }
    usort($comments, "cmp");
    print_r($comments);
?>

Upvotes: 2

Related Questions