How to search Array for multiple values in PHP?

后端 未结 5 1292
滥情空心
滥情空心 2020-12-03 13:58

I need to get the keys from values that are duplicates. I tried to use array_search and that worked fine, BUT I only got the first value as a hit.

I need to get both

5条回答
  •  离开以前
    2020-12-03 14:08

    You can achieve that using array_search() by using while loop and the following workaround:

    while (($key = array_search("2009-09-09", $list[0])) !== FALSE) {
      print($key);
      unset($list[0][$key]);
    }
    

    Source: cue at openxbox at php.net

    For one-multidimensional array, you may use the following function to achieve that (as alternative to array_keys()):

    function array_isearch($str, $array){
      $found = array();
      foreach ($array as $k => $v) {
        if (strtolower($v) == strtolower($str)) {
          $found[] = $k;
        }
      }
      return $found;
    }
    

    Source: robertark, php.net

提交回复
热议问题