0

I got an array like:

  $array = ['foo'=>['bar' => ['value' => 'test']]];

Now I would like to get the value:

echo $array['foo']['bar']['value']; // 'test'

Now my problem begins: Sometimes the array is missing some parts. Like bar. Now I would get an error, because the index bar does not exist.

I can do an array_key_exists search in this. But I'd also have to do this on foo and value.

So my question is: Is there a good way to check the "chain" of array indices?

My first try was something like

strpos(json_encode($array), 'value') > 0

But this is returns a false positive if an array like

  $array = ['foo'=>['value' => 'test']];
  echo $array['foo']['bar']['value']; // error

comes up.

SPQRInc
  • 162
  • 4
  • 23
  • 64

0 Answers0