My PHP code has several PHP variables like this.
$a0 = $arr[0]['id'];
$a1 = $arr[1]['id'];
$a2 = $arr[2]['id'];
$a3 = $arr[3]['id'];
$a4 = $arr[4]['id'];
I want to create these PHP variables using a for loop
This is what I tried so far. But it’s not working.
for($i=0; $i<5; $i++)
{
$a.''.$i = $arr [$i]['id'];
}
Can anyone please help me?
Simply enclose in brackets {}
for($i=0; $i<5; $i++)
{
${'a'.$i} = $arr[$i]['id'];
}
Now
echo $a0; //$arr[0]['id'];
echo $a1; //$arr[1]['id'];
echo $a2; //$arr[2]['id'];
echo $a3; //$arr[3]['id'];
echo $a4; //$arr[4]['id'];
for($i=0; $i<5; $i++)
{
$name = 'a' . $i;
$$name = $arr [$i]['id'];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With