2011-10-14 66 views
0

這是我的數組。根據多維數組中的鍵對數組進行排序並保留其他鍵值。 PHP

 
Array 
     (
      [Data] => Array 
       (
        [0] => Array 
         (
          [recipeid] => 108 
          [recipe] => Rasams- the tongue ticklers ! 
          [image] => No data 
          [category] => Rasams and Soups 
         ) 

        [1] => Array 
         (
          [recipeid] => 44 
          [recipe] => Brain Booster- do you want to try it? 
          [image] => brain-booster-do-you-44-HP-62.jpg 
          [category] => Drinks and Smoothies 
         ) 

        [2] => Array 
         (
          [recipeid] => 36 
          [recipe] => Pineapple Grape Smoothy--a rare combo 
          [image] => pineapple-grape-smoo-36-HP-62.jpg 
          [category] => Drinks and Smoothies 
         ) 

       ) 

     ) 

我有排序根據[鍵]配方的值的字母順序[DATA]陣列,還保存排序後的recipeid,圖像,類別。

回答

0

使用usort。

usort($yourarray['Data'], 'data_sort'); 

function data_sort($a, $b) { 
    return (strcasecmp($a['recipe'], $b['recipe']) > 0); 
} 
0

你應該可以用usort()來做到這一點。下面是一個如何完成的例子,基於PHP文檔中給出的例子。

function cmp($a, $b) 
{ 
    if ($a['recipe'] == $b['recipe']) { 
     return 0; 
    } 
    return ($a['recipe'] < $b['recipe']) ? -1 : 1; 
} 

usort($a, "cmp");