I can make a variable’s name from two variables’ value:
$a = 'tea';
$b = 'pot';
$tea_pot = 'Too Hot';
Here if I print or echo ${$a.'_'.$b}, I will get ‘Too Hot’ as output.
Similarly I need to make an array’s name from two variables’ value.
I try this the code below. But it’s giving me an error: Undefined variable.
I want something like:
$k = ${'B'.$a} where $a=5 and $B5 = array('c', 'v', 'b');
and want to echo($k[2]);
the codes that i want to solve is here…
$B0 = array('apple', 'crow', 'monkey', 'lion', 'deer', 'bear', 'tiger');
$B1 = array('bad', 'meaningless', 'odd', 'no fare', 'poor', 'old', 'dirty', 'damn', 'rush');
function($set, $number){
while($set >= 2){
$set -= 2;
}
if($set == 0){
while($number >= 7){
$numbur -= 7;
}
} else {
while($number >= 9){
$numbur -= 9;
}
}
$array = ${'B'.$set}; // Error
return $array[$number];
}
/*Error: it is saying that here the "${'B'.$set}" is a Undefined variable
where it is a name of an array. what can I do to realize
that it is not a variable but an array.*/
I got the real problem and its solve…
Here the arrays “B0” and “B1” are places outside the function so you have to declare the ${‘B’.$set} as a global array by just adding a single line inside the function before you call it. Like this:
now it will understand and will get the array properly and will not show any errors.