1

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 ?

0

1 Answer 1

5

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!

Sign up to request clarification or add additional context in comments.

2 Comments

Great ! Thats working. I cant accept the answer yet but will when the time limit is up. Thanks !
The best part of the answer is to AVOID VARIABLE VARIABLES! If you can create a separate array with the names of the arrays, you can also create one big array with the other arrays included. If all else fails, go for $var1 = ['array1' => $array1, 'array2' => $array2,];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.