2

How would I re-order an array of objects like:

Array
(
    [0] => stdClass Object
        (
            [term_id] => 3
            [name] => Name
        )
    [1] => stdClass Object
        (
            [term_id] => 1
            [name] => Name2
        )
    [2] => stdClass Object
        (
            [term_id] => 5
            [name] => Name
        )
)

According to the objects term_id, against a custom defined array of ids:

$order_by = array( 5,3,1 )

What I'm using now is below, but I feel like I'm not taking advantage of some advanced sorting functions PHP has... Can anyone tell me what would work better?

$sorted_terms = array();
$order_by = array( 5,3,1 );

foreach( $order_by as $id ) {
    foreach ( $terms as $pos => $obj ) {
        if ( $obj->term_id == $id ) {
            $sorted_terms[] = $obj;
            break;
        }
    }
}
1
  • You can use php's usort method with a custom comparison function as parameter. Commented Dec 3, 2012 at 22:15

2 Answers 2

6

Basically, the rank of your terms in their sorted order is the same as the rank of their corresponding ids, which happens to be the array keys in the $order_by array. So we just need to flip that array to get the mapping of ids to ranks, and then sort using it with a custom comparison function.

Here's a simple code snippet that should work:

<?php

   $skeys = array_flip($order_by);
   usort($terms,
             function($a,$b) use ($skeys){$a =  $skeys[$a->term_id]; $b = $skeys[$b->term_id]; return $a - $b;});

The above code would work with PHP 5.3 or later.

Here's the same thing with pre 5.3 PHP:

 <?php

   $skeys = array_flip($order_by);
   function sfun($a,$b){
       global $skeys;
       $a =  $skeys[$a->term_id]; $b = $skeys[$b->term_id]; return $a - $b;
   }

   usort($terms, "sfun");

The important functions are:

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

1 Comment

Thanks, this sorts the array, but not by the term_id property. I'm playing with your code to see if I can modify it to work :)
1

Here is comparision of available PHP sorting functions. In your case, you need something with your own comparator - one of these with "order of sort" marked as "user defined", most likely uasort() or usort()

EDIT

Custom comparator have to return either -1 or 0 or +1. But it is up to comparator to decide what it does really mean for two object to be equal or less or greater than each other and what logic rules are used to judge. So you just have to program your logic and i.e. return +1 if object A is bigger than B by rules of your logic. It does NOT mean that A is bigger in mathematical sense. If I compare apple with bike and my comparator returns +1, then it means apple is greater than bike. I said so. My logic. Period.

So, basically, if you can turn your logic into algorithm, then you can write a code that guided by that logic/algorithm can server as your comparator to deliver order you want.

4 Comments

I think uasort() is the one I would need. I just get confused by the comparison function - in the documented examples they use simple greater than or less than comparisons to return a asc or desc array.. which is fine and useful in some cases, but how would I incorporate the custom order?
Added more explanation to the answer.
What does the A and B variables contain? the current and next array position?
Two array elements (in your case two objects). It is irrelevant if they are next to each other or not. You just need to compare them - you compare objects NOT their position in container. NOTE: it may be that this is not acceptable approach to sorting in your case, but since it works for 99% cases, that's why it works here that way.

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.