up vote 1 down vote favorite

Hi, I have one array that contains some settings that looks like basically like this:

$defaults = array(
   'variable' => 'value', 
   'thearray' => array(
                  'foo' => 'bar'
                  'myvar' => array('morevars' => 'morevalues');
                  );
);

On another file, i get a string with the first level key and it's childs to check if there is a value attached to it. Using the array above, i'd get something like this:

$option = "thearray['myvar']['morevars']";

I need to keep this string with a similar format to the above because I also need to pass it to another function that saves to a database and having it in an array's format comes in handy.

My question is, having the array and the string above, how can i check for both, existance and value of the given key inside the array? array_key_exists doesn't seem to work below the first level.

link|flag

2 Answers

up vote 1 down vote

You could use a simple function to parse your key-string and examine the array like:

function array_deep_exists($array, $key)
{
    $keys = preg_split("/'\\]|\\['/", $key, NULL, PREG_SPLIT_NO_EMPTY);
    foreach ($keys as $key)
    {
        if ( ! array_key_exists($key, $array))
        {
            return false;
        }
        $array = $array[$key];
    }

    return true;
}

// Example usage
$defaults = array(
    'variable' => 'value', 
    'thearray' => array(
        'foo' => 'bar',
        'myvar' => array('morevars' => 'morevalues')
    )
);
$option = "thearray['myvar']['morevars']";
$exists = array_deep_exists($defaults, $option);
var_dump($exists);  // bool(true)

Finally, to get the value (if it exists) return $array where the above returns true.

Note that if your array might contain false, then when returning the value you'll have to be careful to differentiate no-matching-value from a successful false value.

link|flag
That works, thanks a lot. I'm not sure i like that it depends on the format of the string being exact but i guess that is the nature of the problem. I noticed the false values problem, but they are using 0 for those anyway, so that won't be a problem. – BioXD Aug 23 at 20:25
It only depends on the string format being exact if you want it to. It would be trivial to change the code which assigns the list of keys accept any other format(s). – salathe Aug 23 at 22:51
up vote -1 down vote

You need to eval this code, and use isset function in an eval string, and don't forget to add $ character in right place before code eval

example:

eval("echo isset(\$defaults['varname']['varname2']);")

this will echo 0 or 1 (false or true) You can do anything in eval, like a php source

link|flag
You should avoid using eval() whenever possible, especially when handling data from external sources, as it is a very major security risk (possibility of code injection), and therefore this is a bad solution. – Frxstrem Aug 23 at 20:05
i know it's a bad solution, but situation exampled in a question is even more bad technique to do things, if it's done that way, i assume it's done by purpes and knowing what are threats of this kind of programming... solution i proposed just works, it may not be a perfect one ... – canni Aug 23 at 20:21

Your Answer

 
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.