1

I have such code:

$data = [];
$data['key1']['key2']['key3'] = 123;

and then, I need to get data from this array is such or similar way:

$path = "['key1']['key2']['key3']";
echo $data[$path];  # should return 123

Any ideas?

Thanks!

Vlad
  • 73
  • 1
  • 10
  • Did you tried anything or should we just write your code?! – Rizier123 Jan 29 '15 at 16:24
  • How are you building the string up in the first place? – Martin Bean Jan 29 '15 at 16:24
  • PHP arrays don't work like that - you can't do it this way. What are you trying to accomplish? – Dan Smith Jan 29 '15 at 16:25
  • If you know the key names such that you can put them in the string, then you should just access the array properly instead of with a special string. – Brian Warshaw Jan 29 '15 at 16:25
  • 2 Rizier123: yes, i've tried to solve this by creating simple class and use __get function, but still nothing. – Vlad Jan 29 '15 at 16:27
  • $path is index of your 1D array. it will work `$path = "['key1']['key2']['key3']";$data[$path]='xx'; echo $data[$path]` but `$data[$path]!=$data['key1']['key2']['key3']` `(1D!=3D)` – Shaiful Islam Jan 29 '15 at 16:33

0 Answers0