用鍵值對PHP中對像數組的數組進行排序



Answers

100%從本頁上的第一個答案中被盜。 http://us.php.net/manual/en/function.array-multisort.php
但這正是我所期待的。

multisort一個對像數組:

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
         )

   )
)
Question

基本上我有一個像下面這樣的設置:

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 )
              )
  )

我需要的是一種方法,通過對像中的[value]鍵對父數組中的所有數組進行排序。 現在我已經用了大約2天的時間用不同的方法嘗試了,但是我似乎無法讓我的頭腦發現。 [value]鍵的範圍從0到100,我需要所有的數組以降序排列(IE:100降到0)。




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

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

為了排序基於除簡單的值或鍵以外的任何數組,您需要使用usort函數並提供您自己的比較。 必須定義比較函數,如果$a$b之前,則返回正值,如果$a $b$b之前,則返回負值(如果相等,則為零)。 如果您正在根據數值進行比較,而您想要進行反向排序,則最簡單的方法是從$b的值中減去$a的“值”。






Links