2014-07-14 42 views
0

對象數組排序,我有這樣的數組:如何通過價值內心深處陣列

stdClass Object 
(
    [tid] => 26001835 
    [vid] => 5 
    [name] => AppleTV 
    [description] => My description 
    [format] => filtered_html 
    [weight] => 0 
    [vocabulary_machine_name] => how_to_watch_device 
    [field_device_image] => Array 
     (
      [und] => Array 
       (
        [0] => Array 
         (
          [fid] => 26608990 
          [alt] => 
          [title] => 
          [width] => 194 
          [height] => 102 
          [uid] => 26000697 
          [filename] => Apple-TV.png 
          [uri] => public://Apple-TV.png 
          [filemime] => image/png 
          [filesize] => 2103 
          [status] => 1 
          [timestamp] => 1405346182 
         ) 

       ) 

     ) 

    [field_buy_now_button_link] => Array 
     (
      [und] => Array 
       (
        [0] => Array 
         (
          [value] => http://www.something.com 
          [format] => 
          [safe_value] => http://www.something.com 
         ) 

       ) 

     ) 

    [field_learn_more] => Array 
     (
      [und] => Array 
       (
        [0] => Array 
         (
          [value] => http://something.com/somepage 
          [format] => 
          [safe_value] => http://something.com/somepage 
         ) 

       ) 

     ) 

    [field_device_category] => Array 
     (
      [und] => Array 
       (
        [0] => Array 
         (
          [value] => network 
         ) 

       ) 

     ) 

) 
stdClass Object 
(
    [tid] => 26001834 
    [vid] => 5 
    [name] => Playstation - USA 
    [description] => My description 
    [format] => filtered_html 
    [weight] => 2 
    [vocabulary_machine_name] => how_to_watch_device 
    [field_device_image] => Array 
     (
      [und] => Array 
       (
        [0] => Array 
         (
          [fid] => 26608991 
          [alt] => 
          [title] => 
          [width] => 194 
          [height] => 102 
          [uid] => 26000697 
          [filename] => ps4network.png 
          [uri] => public://ps4network.png 
          [filemime] => image/png 
          [filesize] => 4566 
          [status] => 1 
          [timestamp] => 1405346218 
         ) 

       ) 

     ) 

    [field_buy_now_button_link] => Array 
     (
      [und] => Array 
       (
        [0] => Array 
         (
          [value] => http://www.somesite.com 
          [format] => 
          [safe_value] => http://somesite.com 
         ) 

       ) 

     ) 

    [field_learn_more] => Array 
     (
     ) 

    [field_device_category] => Array 
     (
      [und] => Array 
       (
        [0] => Array 
         (
          [value] => blast_areas 
         ) 

       ) 

     ) 

) 
stdClass Object 
(
    [tid] => 26001836 
    [vid] => 5 
    [name] => Brighthouse Networks 
    [description] => My description 
    [format] => filtered_html 
    [weight] => 3 
    [vocabulary_machine_name] => how_to_watch_device 
    [field_device_image] => Array 
     (
      [und] => Array 
       (
        [0] => Array 
         (
          [fid] => 26608993 
          [alt] => 
          [title] => 
          [width] => 194 
          [height] => 102 
          [uid] => 26000697 
          [filename] => brighthouse.png 
          [uri] => public://brighthouse.png 
          [filemime] => image/png 
          [filesize] => 8392 
          [status] => 1 
          [timestamp] => 1405358781 
         ) 

       ) 

     ) 

    [field_buy_now_button_link] => Array 
     (
     ) 

    [field_learn_more] => Array 
     (
     ) 

    [field_device_category] => Array 
     (
      [und] => Array 
       (
        [0] => Array 
         (
          [value] => ppv_provider 
         ) 

       ) 

     ) 

) 

我想陣列由陣列由field_device_category值排序。基本上,我想對結果進行分組,但首先我需要確保所有對象都按照field_device_category排序。

提前致謝!

回答

0

使用usort進行這種分類。它在後臺使用Quicksort並採用用戶定義的函數來比較陣列元素:

usort($array, "complicatedArrayComparer"); 
function complicatedArrayComparer($a,$b) 
{ 
    if ($a['field_device_category'] == $b['field_device_category']) { 
     return 0; 
    } 
    return ($a['field_device_category'] < $b['field_device_category']) ? -1 : 1; 
} 
+0

有趣。 ['field_device_category']的條目需要定位到['field_device_category']出現在數組中的位置,是否正確? 類似於 - > field_device_category ['und'] [0] ['value'],對不對? – Hochmania

+0

沒錯。您只需將數組元素作爲函數的參數。你唯一要做的就是比較哪一個數組元素更大,並返回1(第一個更大),0(相等)或-1(第二個)。 –