Answer the question
In order to leave comments, you need to log in
How to access a multidimensional array given a string?
there is an array array('one' => array('two'=>'kek'));
and a string $string = '[one][two]';
how to access an array by string?
Answer the question
In order to leave comments, you need to log in
$a = array('one' => array('two'=>'kek'));
$string = '[one][two]';
preg_match_all('/(\+\])/',$string,$matches);
$a[$mathes[0][0]][$mathes[0][1]];
Universal solution for a large number of keys
$arr = ['one' => ['two' => 'kek']];
$key = '[one][two]';
function arr_key_val($arr, $key) {
$keys = trim($key, '[]');
$keys = explode('][', $keys);
$return = $arr;
foreach ($keys as $k)
$return = $return[$k];
return $return;
}
echo arr_key_val($arr, $key);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question