简体   繁体   English

在数组内搜索数组

[英]Searching an array inside an array

I have an array, inside an array. 我有一个数组,在数组内部。 I'd like to search the values in that nestled array. 我想搜索该嵌套数组中的值。

Currently I'm trying: 目前,我正在尝试:

    foreach($retval as $k=>$v){

          if (is_array($v)){

            $search = array_search($group_name,$v);

          }

     }

     if($search == FALSE) {

        // Nothing was found

     } else {

          // results found

      }

Once this has been done, I simply want to perform an action depending on whether a result was found or not in the search. 完成此操作后,我只想根据是否在搜索中找到结果来执行操作。

How do you do this? 你是怎么做到的?

You need to change $search = array_search($group_name,$v); 您需要更改$search = array_search($group_name,$v); to: 至:

$search = false;
foreach($retval as $k=>$v){
    if(array_search($group_name,$v)){
        $search = true;
    }
}

Basically, you only want to assign true to search if you found the value you are looking for. 基本上,只要找到要查找的值,就只想将true分配给search。 Otherwise you could overwrite search's value with false. 否则,您可以使用false覆盖搜索的值。 For example, say search is in element 0, you set it to true. 例如,假设搜索位于元素0中,则将其设置为true。 Then in element 1 the element is not there, you then set search to false. 然后在元素1中,该元素不存在,然后将search设置为false。

Furthermore, if you only care about knowing it's there, you could add break; 此外,如果您只想知道它的存在,则可以添加break; after $search = true; $search = true; to stop searching the array. 停止搜索数组。

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

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