简体   繁体   English

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

[英]How can I sort the results of a for each loop?

I have a list of strings var_dump($key) : 我有一个字符串列表var_dump($key)

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

They are a result of a loop from a multidimensional array: 它们是来自多维数组的循环的结果:

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

Is it possible to sort this list of strings? 是否可以对此字符串列表进行排序?

I tried: 我试过了:

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

and also 并且

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

My sort attemts are not working. 我的排序不起作用。 The problem is I cannot sort the array because it is a complex multidimensional array. 问题是我无法对array排序,因为它是一个复杂的多维数组。 So I somehow only want to sort the results. 所以我不知何故只想对结果进行排序。 But I am not sure how to do that. 但我不知道该怎么做。

The result I wish for var_dump($key) would be: 我希望var_dump($key)结果是:

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

PHP - Sort Functions For Arrays 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM