How I can use variable variables with array to get result like is below?
I've tried so far:
// $g_module_id_bar_1['id'] = 5;
$i = 1;
$variablename = 'g_module_id_bar_'.$i;
$key = '\'id\'';
echo $$variablename[$key];
Result should be: 5
How I can use variable variables with array to get result like is below?
I've tried so far:
// $g_module_id_bar_1['id'] = 5;
$i = 1;
$variablename = 'g_module_id_bar_'.$i;
$key = '\'id\'';
echo $$variablename[$key];
Result should be: 5
You almost had it.
Change $key = '\'id\'';
too $key='id';
The reason is because PHP understands that $key
contains a string. When accessing an array noramlly, you wouldn't do something like:
<?php
$var = array("hello"=>"world");
echo $var["'hello'"];
which is effectively what you were doing
See for full solution: https://3v4l.org/qk5ZL