1

The question is whether it's possible to address a multidimensional array with a string or another array. Say we have:

$a['key1']['key2'] = "value"
//and
$keyArray = array('key1', 'key2')
//or 
$keyString = 'key1,key2'

Is it possible to do something like:

$a[$keyArray] 
//or
$a[keyString]

which would then give me back the value "value"?

Obviously, what I wrote doesn't work (not on my system, at least), but is something similar possible and if yes, how?

Thanks.

5
  • Will $keyArray always contain the indices of $a or will it contain more data than just that? Commented Dec 15, 2016 at 14:31
  • $a[ $keyArray[0] ][ $keyArray[1] ] , this way is possible, but for second one u have to seperate them Commented Dec 15, 2016 at 14:35
  • That is not very dynamic, hence my first comment Commented Dec 15, 2016 at 14:37
  • $keyArray hould only contain indices, like in the example. Commented Dec 15, 2016 at 14:44
  • As far as I know there isn't any built-in syntax or function to do that. You could write your own function though. Commented Dec 15, 2016 at 15:00

3 Answers 3

1

As far as I'm aware this isn't possible. However I feel like http://php.net/manual/en/class.arrayiterator.php might be help you answer your question

Sign up to request clarification or add additional context in comments.

1 Comment

iterating probably is the only method to do it dynamically, regardless of the dept of the array. I was hoping for a shortcut :-)
1

If you want a one liner:

$a['key1']['key2'] = "value";

$keyArray = array('key1', 'key2');

$value=$a;foreach ($keyArray as $key) $value=$value[$key];

echo $value;

You should note that this obviously doesn't check if the keys exist.

6 Comments

Absolutely wonderful piece of code! I was trying to achieve the same, however, I could not figure out how to get the indices correctly.. my approach was variables variable.
If you want to check whether the array key exists and whether or not the value is of type arrayif (is_array($value) && array_key_exists($key, $value)) { $value=$value[$key]; }
That's ok. But what happens when the key doesn't exist? Easier not to check at all and let it raise an exception. After all, if the key doesn't exist, it's probably an error condition or some other problem in the code.
Agreed. As that sample would basically just stop at the last valid index, and then do nothing after that. Or if you ended in a situation where you skipped a key and it kept running through and by mistake got a valid key out of that... therefore an else to the if, where you handle the error would be best.
well, this obviously does what I want, but it's iterating. I was hoping to find some shortcut. Seems this is the best that can be done.
|
0

Obviously you cannot pass two indices using some string or one array value. But if you need to use it that way then one way around can be like:

$a['key1']['key2'] = "value"
$keyArray = array('key1', 'key2');
$a[$keyArray[0]][$keyArray[1]]//'value'

That's just in case you are somehow receiving key values in $keyArray and you can access $a only through that otherwise direct access is most convenient:

$a['key1']['key2']//'value'

I hope it helps

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.