1

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
        )
)
psot
  • 194
  • 11
  • You can do it in query itself. – Kumar V Jan 07 '14 at 04:25
  • 2
    possible duplicate of [PHP Sort Array By SubArray Value](http://stackoverflow.com/questions/2477496/php-sort-array-by-subarray-value) – Kumar V Jan 07 '14 at 04:29
  • The query is dependant on the parent comments ID to find the replies. I have tried my best, but it doesn't seem to be the way forward with limited query methods in Wordpress. – psot Jan 07 '14 at 04:32

2 Answers2

2

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);
?>
Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106
  • Thank you very much. Your answer is of great value to me helping me to get a better grasp of array operations. There is a flaw in my query which creates empty arrays like `Array ( )` whenever a parent comment has no children. These empty arrays get mixed in between those containing my data and break the merging of arrays so that only those arrays before the first empty array are included. I will try to fix that and and then go back to your suggestion here and see if I get it to work. – psot Jan 07 '14 at 16:23
0

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...
    }
});
Scopey
  • 6,269
  • 1
  • 22
  • 34
  • Thank you! I believe the best solution for me is to try to get the merging of arrays to work. – psot Jan 07 '14 at 16:30