如何为每个循环排序a的结果?

时间:2016-05-12 09:56:59

标签: php arrays sorting multidimensional-array

我有一个字符串列表var_dump($key)

string(6) "samuel"
string(4) "john"
string(4) "alan"
string(5) "frank"
string(3) "bob"

它们是来自多维数组的循环的结果:

foreach ($array as $key => $item) { 
   if(is_array($item)){
      if (stripos($key, $keySearch) !== false){
      var_dump($key);
      }
   }
}

是否可以对此字符串列表进行排序?

我试过了:

   foreach ($array as $key => $item) {  
       if(is_array($item)){
          if (stripos($key, $keySearch) !== false){
          asort($key);
          var_dump($key);
          }
       }
    }

以及

foreach ($array as $key => $item) {  
       if(is_array($item)){
          if (stripos($key, $keySearch) !== false){
              array_multisort($key);
              var_dump($key);
          }
       }
    }

我的排序不起作用。问题是我无法对array进行排序,因为它是一个复杂的多维数组。所以我不知何故只想对结果进行排序。但我不知道该怎么做。

我希望var_dump($key)的结果是:

string(4) "alan"
string(3) "bob"
string(5) "frank"
string(4) "john"
string(6) "samuel"

1 个答案:

答案 0 :(得分:0)

PHP - 为数组排序函数

sort() - sort arrays in ascending order
rsort() - sort arrays in descending order
asort() - sort associative arrays in ascending order, according to the value
ksort() - sort associative arrays in ascending order, according to the key
arsort() - sort associative arrays in descending order, according to the value
krsort() - sort associative arrays in descending order, according to the key