How to use PHP in_array with associative array?

前端 未结 6 1667
夕颜
夕颜 2020-12-03 00:15

Is there any php function such as in_array for associative arrays you get by the mysql function \"mysql_fetch assoc\" ?

For example, if I have an $array that looks l

相关标签:
6条回答
  • 2020-12-03 00:33

    The sample formule, using class and methods:

    class VerifyInArray
    {
     public function getMyCollection($field, $collection)
     {
         $list = array();
         if (count($collection)) {
            foreach ($collection as $k => $val) {
                $list[] = $val[$field];
            }
         }
         return $list;
     }
    
    public function inMyArray($collection, $field, $findValue)
    {
        if (isset($collection[0])) {
            if (array_key_exists($field, $collection[0]) == false) {
               return 'no'; 
            }
        }
    
        if (in_array($findValue, $this->getMyCollection($field, $collection))) {
            return 'ok';
        }
        return 'no';
    }
    
    public function displayInArray($collection, $attr, $value)
    {
       return 'search result: '. $this->inMyArray($collection, $attr, $value);
    }
    
    }
    $src = new VerifyInArray();
    
     $collection = array(
             array(
                   'ID' => 1, 
                   'name' => 'Smith'
             ), 
             array(
                   'ID' => 2, 
                   'name' => 'John'
             )
        );
    echo $src->displayInArray($collection, 'ID', 2). "\n<br>" .
         $src->displayInArray($collection, 'ID', 0);
    

    Model in ideone

    0 讨论(0)
  • 2020-12-03 00:45

    You can't do it directly on nested arrays.. You need to nest it down a bit and then do it.

    <?php
    $arr=array(0=>array('ID'=>1, 'name'=>"Smith"), 1=>array('ID'=>2, 'name'=>"John"));
    
    foreach($arr as $arr1)
    {
        if(in_array(1,$arr1))
        {
           echo "Yes found.. and the correspoding key is ".key($arr1)." and the employee is ".$arr1['name'];
        }
    }
    

    OUTPUT :

    Yes found.. and the correspoding key is ID and the employee is Smith
    
    0 讨论(0)
  • 2020-12-03 00:47

    First you must know which part of the associative array you're going to use as haystack in in_array function. Then you can use in_array without additional code.

    Example with values :

    <?php
    $assoc = array(1 => "apple", 2 => "banana", 3 => "lemon", 4 => "pear");
    $haystack = array_values($assoc);
    echo "<p>" . print_r($assoc, true) . "</p>";
    
    $needle = 'banana';
    $find = (in_array($needle, $haystack)) ? 'TRUE' : 'FALSE';
    echo "<p>$needle : $find</p>";
    
    $needle = 'cherry';
    $find = (in_array($needle, $haystack)) ? 'TRUE' : 'FALSE';
    echo "<p>$needle : $find</p>";
    ?>
    

    Results in :

    Array ( [1] => apple [2] => banana [3] => lemon [4] => pear )
    
    banana : TRUE
    
    cherry : FALSE
    
    0 讨论(0)
  • 2020-12-03 00:48

    In your case, I wonder if simply using isset() makes more sense, i.e.

    isset($a[$key])
    
    0 讨论(0)
  • 2020-12-03 00:51

    Here is a one liner you can use.

    $isInArray = in_array(1, array_column($names, 'ID'));
    
    0 讨论(0)
  • 2020-12-03 00:56

    Try this..... You can use this function for any depth of the associated array. Just contraint to this function is that the key value would not be repeat any where in array.

    <?php 
    function is_in_array($array, $key, $key_value){
          $within_array = 'no';
          foreach( $array as $k=>$v ){
            if( is_array($v) ){
                $within_array = is_in_array($v, $key, $key_value);
                if( $within_array == 'yes' ){
                    break;
                }
            } else {
                    if( $v == $key_value && $k == $key ){
                            $within_array = 'yes';
                            break;
                    }
            }
          }
          return $within_array;
    }
    $test = array(
                    0=> array('ID'=>1, 'name'=>"Smith"), 
                    1=> array('ID'=>2, 'name'=>"John")
            );
    print_r(is_in_array($test, 'name', 'Smith'));
    ?>
    
    0 讨论(0)
提交回复
热议问题