【问题标题】:How to check if string is in array with php?如何使用php检查字符串是否在数组中?
【发布时间】:2012-03-16 21:34:19
【问题描述】:

当 var_dump 时,我有一个如下所示的数组:

 array(3) { [0]=> array(3) { ["id"]=> string(1) "3" ["category"]=> string(5) "staff" ["num_posts"]=> string(1) "1" } [1]=> array(3) { ["id"]=> string(1) "1" ["category"]=> string(7) "general" ["num_posts"]=> string(1) "4" } [2]=> array(3) { ["id"]=> string(1) "2" ["category"]=> string(6) "events" ["num_posts"]=> string(1) "1" } }

如果数组不包含以下字符串,我需要回显一个值:'hello'

这怎么可能,我尝试过使用 in_array,但没有成功。帮助表示赞赏。

【问题讨论】:

    标签: php arrays string echo


    【解决方案1】:
    foreach ($array as $subarray)
    {
       if(!in_array('hello', $subarray))
       {
          echo 'echo the value';
       }
    }
    

    【讨论】:

      【解决方案2】:

      对于多维数组,试试:

      function in_array_r($needle, $haystack, $strict = true) { foreach ($haystack as $item) { if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) { return true; } } return false; }

      【讨论】:

        【解决方案3】:
        $attendance = ['present','absent','present','present','present','present'];
        
        if (in_array("absent", $attendance)){
        
             echo "student is failed";
        
          } else {
             
              echo "student is passed";    
           }
        

        【讨论】:

          【解决方案4】:

          试试这个

          $array = array( array("id" => "3","category" => "hello" ,"num_posts" =>  "1" ),
              array( "id"=> "1","category"=> "general" ,"num_posts" => "4" ),
              array( "id"=> "2" ,"category"=> "events","num_posts"=>  "1" ));
          
          foreach($array as $value){
              if(!in_array("hello", $value)){
                  var_dump($value);
              }
          }
          

          它的工作原理

          【讨论】:

            【解决方案5】:

            如果你想在每个维度上搜索,试试这个:

            function value_exists($array, $search) {
                foreach($array as $value) {
                    if(is_array($value)) {
                        if(true === value_exists($value, $search)) {
                            return true;
                        }
                    }
                    else if($value == $search) {
                        return true;
                    }
                }
            
                return false;
            }
            
            if(value_exists($my_array, 'hello')) {
                echo 'ok';
            }
            else {
                echo 'not found';
            }
            

            【讨论】:

              【解决方案6】:
              $isExistHelloInArray = array_filter($array,function($element) {
                  return $element['category'] == 'hello';
              });
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2022-12-18
                • 2017-03-17
                • 1970-01-01
                • 1970-01-01
                • 2017-10-30
                • 1970-01-01
                相关资源
                最近更新 更多