Check if multidimensional run-time array exists

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

Take this array

$config['lib']['template']['engine'] = 'setted';

The function should return true when I call it with:

checkKey('lib','template','engine');
//> Checks if isset $config['lib']['template']['engine']

Please note that my array is not only three-dimensional. It should be able to check even with one size:

checkKey('genericSetting');
//> Returns false becase $c['genericSetting'] isn't setted

I am currently using terrible code eval, I would like to hear a suggestion :)

+3
source share
2 answers
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;
}

Using:

checkKey($config, 'lib', 'template', 'engine');
checkKey($config, 'genericSetting');
+9
source

I created the following two functions to solve the same problem as yours.

check . get_value , . . CakePHP Set:: check().

<?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(
                'ddd' => 'ddd'
            )
        ));

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"
+1

All Articles