简体   繁体   中英

check if association keys exists in a particular array

I'm trying to build a function that checks if there's a value at a particular location in an array:

function ($array, $key) {
  if (isset($array[$key]) {
    return true;
  }
  return false;
}

but how can I accomplish this in a multi array? say I want to check if a value is set on $array[test1][test2]

Pass an array of keys, and recurse into the objects you find along the way:

function inThere($array, $keys)
{
    $key = $keys;  // if a single key was passed, use that
    $rest = array();

    // else grab the first key in the list
    if (is_array($keys))
    {
        $key = $keys[0];
        $rest = array_slice($keys, 1);
    }

    if (isset($array[$key]))
    {
        if (count($rest) > 0)
            return inThere($array[$key], $rest);
        else
            return true;
    }

    return false;
}

So, for:

$foo = array(   
  'bar' => array( 'baz' => 1 )
);

inThere($foo, 'bar');                 // == true
inThere($foo, array('bar'));          // == true
inThere($foo, array('bar', 'baz'));   // == true
inThere($foo, array('bar', 'bazX'));  // == false
inThere($foo, array('barX'));         // == false

Here is a non-recursive way to check for if a multi-level hashtable is set.

// $array it the container you are testing.
// $keys is an array of keys that you want to check. [key1,key2...keyn]
function ($array, $keys) {
  // Is the first key set?
  if (isset($array[$key]) {
    // Set the test to the value of the first key.
    $test = $array[$key];
    for($i = 1; $i< count($keys); $i++){
      if (!isset($test[$keys[$i]]) {
        // The test doesn't have a matching key, return false
        return false;
      }
      // Set the test to the value of the current key.
      $test = $test[$keys[$i]];
    }
    // All keys are set, return true.
    return true;
  } else {
    // The first key doesn't exist, so exit.
    return false;
  }
}

I'm headed out, but maybe this. $keys should be an array even if one, but you can alter the code to check for an array of keys or just one:

function array_key_isset($array, $keys) {                                                                                          
    foreach($keys as $key) {
        if(!isset($array[$key])) return false;                                                          
        $array = $array[$key];
    }
    return true;
}
array_key_isset($array, array('test1','test2'));

There's more universal method but it might look odd at first: Here we're utilizing array_walk_recursive and a closure function:

$array = array('a', 'b', array('x', 456 => 'y', 'z'));

$search = 456; // search for 456
$found = false;

array_walk_recursive($array, function ($value, $key) use ($search, &$found)
{
    if ($key == $search)
        $found = true;

});

if ($found == true)
  echo 'got it';

The only drawback is that it will iterate over all values, even if it's already found the key. This is good for small array though

While I probably wouldn't build a function for this, perhaps you can put better use to it:

<?php

function mda_isset( $array )
{
    $args = func_get_args();
    unset( $args[0] );

    if( count( $args ) > 0 )
    {
        foreach( $args as $x )
        {
            if( array_key_exists( $x, $array ) )
            {
                $array = $array[$x];
            }
            else
            {
                return false;
            }
        }
        if( isset( $array ) )
        {
            return true;
        }
    }
    return false;
}

?>

You can add as many arguments as required:

// Will Test $array['Test1']['Test2']['Test3']
$bool = mda_isset( $array, 'Test1', 'Test2', 'Test3' );

It will first check to make sure the array key exists, set the array to that key, and check the next key. If the key is not found, then you know it doesn't exist. If the keys are all found, then the value is checked if it is set.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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