Check if a “run-time” multidimensional array key exists

后端 未结 2 1782
野的像风
野的像风 2020-12-04 03:07

I have a multidimensional array. I need a function that checks if a specified key exists.

Let\'s take this array

$config[\'lib\'][\'template\'][\'eng         


        
相关标签:
2条回答
  • 2020-12-04 03:09

    I created the following two functions to do solve the same problem you are having.

    The first function check is able to check for one/many keys at once in an array using a dot notation. The get_value function allows you to get the value from an array or return another default value if the given key doesn't exist. There are samples at the bottom for basic usage. The code is mostly based on CakePHP's Set::check() function.

    <?php
    
    function check($array, $paths = null) {
        if (!is_array($paths)) {
            $paths = func_get_args();
            array_shift($paths);
        }
    
        foreach ($paths as $path) {
            $data = $array;
    
            if (!is_array($path)) {
                $path = explode('.', $path);
            }
    
            foreach ($path as $i => $key) {
                if (is_numeric($key) && intval($key) > 0 || $key === '0') {
                    $key = intval($key);
                }
    
                if ($i === count($path) - 1 && !(is_array($data) && array_key_exists($key, $data))) {
                    return false;
                }
    
                if (!is_array($data) || !array_key_exists($key, $data)) {
                    return false;
                }
    
                $data =& $data[$key];
            }       
        }
    
        return true;
    }
    
    function get_value($array, $path, $defaultValue = FALSE) {    
        if (!is_array($path))
            $path = explode('.', $path);
    
        foreach ($path as $i => $key) {
            if (is_numeric($key) && intval($key) > 0 || $key === '0')
                $key = intval($key);
    
            if ($i === count($path) - 1) {
                if (is_array($array) && array_key_exists($key, $array))
                    return $array[$key];
                else
                    break;
            }
    
            if (!is_array($array) || !array_key_exists($key, $array))
                break;
    
            $array = & $array[$key];
        }
    
        return $defaultValue;
    }
    
    // Sample usage
    $data = array('aaa' => array(
                'bbb' => 'bbb',
                'ccc' => array(
                    'ffffd' => 'ffffd'
                )
            ));
    
    var_dump( check($data, 'aaa.bbb') ); // true
    var_dump( check($data, 'aaa.bbb', 'aaa.ccc') ); // true
    var_dump( check($data, 'zzz') ); // false
    var_dump( check($data, 'aaa.bbb', 'zzz') ); // false
    
    var_dump( get_value($data, 'aaa.bbb', 'default value') ); // "bbb"
    var_dump( get_value($data, 'zzz', 'default value') ); // "default value"
    
    0 讨论(0)
  • 2020-12-04 03:22
    function checkKey($array) {
      $args = func_get_args();
      for ($i = 1; $i < count($args); $i++) {
        if (!isset($array[$args[$i]]))
           return false;
        $array = &$array[$args[$i]];
      }
      return true;
    }
    

    Usage:

    checkKey($config, 'lib', 'template', 'engine');
    checkKey($config, 'genericSetting');
    
    0 讨论(0)
提交回复
热议问题