Check if a specific value exists at a specific key in any subarray of a multidimensional array

前端 未结 13 2101
梦毁少年i
梦毁少年i 2020-11-27 18:42

I need to search a multidimensional array for a specific value in any of the indexed subarrays.

In other words, I need to check a single column of the multidimension

相关标签:
13条回答
  • 2020-11-27 19:08

    I wrote the following function in order to determine if an multidimensional array partially contains a certain value.

    function findKeyValue ($array, $needle, $value, $found = false){
        foreach ($array as $key => $item){
            // Navigate through the array completely.
            if (is_array($item)){
                $found = $this->findKeyValue($item, $needle, $value, $found);
            }
    
            // If the item is a node, verify if the value of the node contains
            // the given search parameter. E.G.: 'value' <=> 'This contains the value'
            if ( ! empty($key) && $key == $needle && strpos($item, $value) !== false){
                return true;
            }
        }
    
        return $found;
    }
    

    Call the function like this:

    $this->findKeyValue($array, $key, $value);
    
    0 讨论(0)
  • 2020-11-27 19:10

    Nothing will be faster than a simple loop. You can mix-and-match some array functions to do it, but they'll just be implemented as a loop too.

    function whatever($array, $key, $val) {
        foreach ($array as $item)
            if (isset($item[$key]) && $item[$key] == $val)
                return true;
        return false;
    }
    
    0 讨论(0)
  • 2020-11-27 19:16

    As in your question, which is actually a simple 2-D array wouldn't it be better? Have a look-

    Let say your 2-D array name $my_array and value to find is $id

    function idExists($needle='', $haystack=array()){
        //now go through each internal array
        foreach ($haystack as $item) {
            if ($item['id']===$needle) {
                return true;
            }
        }
        return false;
    }
    

    and to call it:

    idExists($id, $my_array);
    

    As you can see, it actually only check if any internal index with key_name 'id' only, have your $value. Some other answers here might also result true if key_name 'name' also has $value

    0 讨论(0)
  • 2020-11-27 19:18

    Here is an updated version of Dan Grossman's answer which will cater for multidimensional arrays (what I was after):

    function find_key_value($array, $key, $val)
    {
        foreach ($array as $item)
        {
            if (is_array($item) && find_key_value($item, $key, $val)) return true;
    
            if (isset($item[$key]) && $item[$key] == $val) return true;
        }
    
        return false;
    }
    
    0 讨论(0)
  • 2020-11-27 19:18

    The simplest way is this:

    $my_array = array(    
        0 =>  array(  
            "name"   => "john",  
            "id"    =>  4  
        ),  
        1   =>  array(  
            "name" =>  "mark",  
            "id" => 152  
        ), 
        2   =>  array(  
            "name" =>  "Eduard",  
            "id" => 152  
        )
    );
    
    if (array_search(152, array_column($my_array, 'id')) !== FALSE)
      echo 'FOUND!';
    else
      echo 'NOT FOUND!';
    
    0 讨论(0)
  • 2020-11-27 19:20
    function checkMultiArrayValue($array) {
            global $test;
            foreach ($array as $key => $item) {
    
                if(!empty($item) && is_array($item)) {
                    checkMultiArrayValue($item);
                }else {
                    if($item)
                     $test[$key] = $item;
    
                }
            }
            return $test;   
        }
    
     $multiArray = array(    
                    0 =>  array(  
                          "country"   => "",  
                          "price"    => 4,  
                          "discount-price" => 0,  
                   ),);
    
    $test = checkMultiArrayValue($multiArray);
    echo "<pre>"
    print_r($test);
    

    Will return array who have index and value

    0 讨论(0)
提交回复
热议问题