Sort an array of an array of objects in PHP by key value

Basically I have a setup like:

Array ( 
[0] => Array ( [0] => stdClass Object ( [nid] => 1 [title] => title1 [uid] => 1 [parent] => 0 [weight] => -15 [name] => name1 [value] => 0 )
               [1] => stdClass Object ( [nid] => 2 [title] => title2 [uid] => 1 [parent] => 0 [weight] => -7 [name] => name2 [value] => 100 )
               [2] => stdClass Object ( [nid] => 3 [title] => title3 [uid] => 2 [parent] => 0 [weight] => -1 [name] => name3 [value] => 0 )
               [3] => stdClass Object ( [nid] => 4 [title] => title4 [uid] => 2 [parent] => 0 [weight] => 1 [name] => name4 [value] => 80 )
              )
  )

I need a way to sort all arrays inside the parent array using the [value] key in the object. I tried for about 2 days using usort and different methods, but I just can't seem to fool it. The key [value] will be in the range from 0 to 100, and I need all arrays sorted in descending order (IE: 100 to 0).

+5
source share
5 answers

Use usort :

function cmp($a, $b) {
  if ($a->value == $b->value) {
    return 0;
  } else {
    return $a->value < $b->value ? 1 : -1; // reverse order
  }
}

usort($arr, 'cmp');
+13
source

100% . http://us.php.net/manual/en/function.array-multisort.php
, .

:

example object [$object with array of objects]: (class: test) 
---------------------------------- 

test Object (
  [Artikel] => Array (
      [0] => test Object (
            [id] => 1
            [title] => CCCC
         )
      [1] => test Object (
            [id] => 2
            [title] => AAAA
         )
      [2] => test Object (
            [id] => 3
            [title] => DDDD
         )
      [3] => test Object (
            [id] => 4
            [title] => BBBB
         )
   )
)

---------------------------------- 

Simple PHP function: sort_arr_of_obj()

<?php 
// -------------------------------------- 

/* 
* -------- function arguments -------- 
*   $array ........ array of objects
*   $sortby ....... the object-key to sort by
*   $direction ... 'asc' = ascending
* --------
*/

function sort_arr_of_obj($array, $sortby, $direction='asc') {

    $sortedArr = array();
    $tmp_Array = array();

    foreach($array as $k => $v) {
        $tmp_Array[] = strtolower($v->$sortby);
    }

    if($direction=='asc'){
        asort($tmp_Array);
    }else{
        arsort($tmp_Array);
    }

    foreach($tmp_Array as $k=>$tmp){
        $sortedArr[] = $array[$k];
    }

    return $sortedArr;

}


// -------------------------------------- 
?>

example call: 
---------------------------------- 

<?php 

$sorted->Artikel = sort_arr_of_obj($object->Artikel,'title','asc');

?>

example result: $sorted (class: test) 
---------------------------------- 

test Object (
  [Artikel] => Array (
      [0] => test Object (
            [id] => 2
            [title] => AAAA
         )
      [1] => test Object (
            [id] => 4
            [title] => BBBB
         )
      [2] => test Object (
            [id] => 1
            [title] => CCCC
         )
      [3] => test Object (
            [id] => 3
            [title] => DDDD
         )

   )
)
+4

- , , . array_multisort . :

<?php
$array1 = $objectvalues
$array2 = array(ObjectWithNid1, ObjectWithNid2, ObjectWithNid3, ObjectWithNid4);
array_multisort($array1, $array2);
?>

foreach []:

<?php
foreach( $arraywithobjects as $obj )
{
    $objectvalues[] = $obj->getValue();
}
?>

Object , .

, :

<?php
foreach( $arraywithobjects as $obj )
{
    $objectvalues[] = $obj->getValue();
}
$array2 = array(ObjectWithNid1, ObjectWithNid2, ObjectWithNid3, ObjectWithNid4);
array_multisort($objectvalues, $array2);
?>

array_multisort , .

. :

+3
function cmp($a, $b) {
    return $b->value - $a->value;
}

$ary[0] = usort($ary[0], "cmp");

- , , usort . , $a $b, , $b $a ( , ). , - "" $a $b.

+1

I could be wrong, but I believe I did something similar using asort () (or asort ()). This was in a search function where I needed to sort a two-dimensional array filled with indexes and timestamps.

I'm not sure if this will work in your case, and I did it a long time ago. Maybe this will help you, good luck.

0
source

All Articles