I need to get values of array through a loop with dynamic vars. I can't understand why the "echo" doesn’t display any result for "$TAB['b']". Do you know why ?
The test with error message : https://3v4l.org/Fp3GT
$TAB_a = "aaaaa";
$TAB['b'] = "bbbbb";
$TAB['b']['c'] = "ccccc";
$TAB_paths = ["_a", "['b']", "['b']['c']"];
foreach ($TAB_paths as $key => $value) {
echo "\n\n\${'TAB'.$value} : "; print_r(${'TAB'.$value});
}
You are treating the array access characters as if they are part of the variable name. They are not.
So if you have an array
$TAB = array('b' => 'something');, the variable name is$TAB. When you do${'TAB'.$value}, you're looking for a variable that's actually named$TAB['b'], which you don't have.Since you say that you just want to be able to access array indexes dynamically based on the values in another array, you just put the indexes alone (without the array access characters) in the other array.
Output:
DEMO