exussum
exussum

Reputation: 18550

Php Variable Variables

Ive got some generated arrays, and their variable names stored in another array like the following

$array1 = 4x119 array;
$array2 = 4x119 array;
etc ..
$var1= [
"array1",
"array2",
etc...
];

and trying to loop though them like this

foreach ($var1 as $loopitem){
var_dump($$loopitem[3]);
}

How can i make this less ambiguous ?

Currenly im fairly sure its looking for a variable called the contents of $loopitem[3] instead of looking at $arr1[3] as without the [3] the var dump returns correct

Without the [3]

array(4) {
  [0]=>
  array(119) { 

rest of output

With [3]

NULL

Any suggestions ?

Upvotes: 1

Views: 106

Answers (1)

ThiefMaster
ThiefMaster

Reputation: 318498

You can use ${$loopitem}[3] to make it readable and unambiguous. Actually I'd always use that syntax for variable variables since $$foo is easy to misread as $foo.

However, it would be even better not to use them at all and use an array instead!

Upvotes: 5

Related Questions