2011-10-14 62 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");