0

In the example are two arrays, but actually may be other number of arrays (i do not know how many arrays i may have).

$some_name = array ( "Volvo_0",220,180, );
$array_name_for_variable[]='some_name';//here i create another array latter to loop through

$another_name = array ( "Volvo_1",221,181, );
$array_name_for_variable[]='another_name';

In the example i just want to print_r the arrays i may have. So i loop through $array_name_for_variable. Like

foreach( $array_name_for_variable as $i_array_name_for_variable => $val_array_name_for_variable ) {

trying to print particular array (like print_r($some_name)), using this

echo '<pre>', print_r($['val_array_name_for_variable'], true), '</pre> $val_array_name_for_variable __<br/>';

but see error Parse error: syntax error, unexpected '[', expecting T_VARIABLE or '$'

}

This print_r($['val_array_name_for_variable']) is wrong. Tried this print_r( $[$val_array_name_for_variable] );. Also got error.

Any ideas what need to change.

Why all this and what i need...

I have 12 arrays, but i do not know which of 12 would be used in one particular page.

So page loads, some of the 12 arrays are defined (used).

I may write like if array_1 exists, then long html code using variables from the array_1.

Then again if array_2 exists and not empty, then repeat the same html code with variables from array_2.

But instead of copy-paste (repeating) html code i decided to loop through arrays existing in opened page and the long html code write only once.

8
  • You can't use $['xxx'] this is because you need a name of the variable meaning you can have $var['xxx'] but not just $['xxx'] as PHP wouldn't know which array you are trying to access with $var['xxx'] it would know you want the value for key xxx in array $var. Commented Oct 19, 2014 at 16:49
  • 1
    its very hard to understand what you trying to achive Commented Oct 19, 2014 at 16:51
  • Would the result be some_name, $val_array_name_for_variable __, another_name and $val_array_name_for_variable __? Commented Oct 19, 2014 at 16:56
  • @Dwza. Edited question and tried to explain why i need all this and what i want. Commented Oct 19, 2014 at 17:00
  • doesn't all arrays exist on page load? Commented Oct 19, 2014 at 17:04

2 Answers 2

1

You could solve the problem with:

$some_name = array ( "Volvo_0",220,180, );
$array_name_for_variable[]=$some_name;

$another_name = array ( "Volvo_1",221,181, );
$array_name_for_variable[]=$another_name;

then iterate through it:

foreach( $array_name_for_variable as $i_array_name_for_variable => $val_array_name_for_variable ) {
echo '<pre>', print_r($val_array_name_for_variable, true), '</pre><br/>';
}

This will print all the elements in $array_name_for_variable.

Comment if you need any more changes to the output.

Explanation: What the code is actually doing is iterating through all elements of the array $array_name_for_variable. They it creates a key => value for each element in it. The value ($val_array_name_for_variable) on the first iteration would be: $some_name. The key ($i_array_name_for_variable) - the element position in the array, so on the first iteration it would be 0 (as it always start from 0).

If you don't need the element position you could do it like this:

foreach( $array_name_for_variable as $val_array_name_for_variable ) {
echo '<pre>', print_r($val_array_name_for_variable, true), '</pre><br/>';
}

It would generate exactly the same output as the previous code snippet.

For adding elements to the array you have to pass a variable and you were passing just a string.

EDIT: Based on the newly added info the code should be:

foreach( $array_name_for_variable as $i_array_name_for_variable => $val_array_name_for_variable ) {
if (array_key_exists($i_array_name_for_variable, $val_array_name_for_variable)) {
echo '<pre>', print_r($val_array_name_for_variable, true), '</pre><br/>';
} else {
//Code if the array does not exist.
}
}

References:

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

Comments

1

You may want to do something like this

$some_name = array ( "Volvo_0",220,180, );
$array_name_for_variable['some_name']= $some_name ;

$another_name = array ( "Volvo_1",221,181, );
$array_name_for_variable['another_name']= $another_name ;


foreach( $array_name_for_variable as $i_array_name_for_variable => $val_array_name_for_variable ) {

   print_r($val_array_name_for_variable);//prints the array
   print_r($i_array_name_for_variable);// print the keys ex :- some_name
}

Comments

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.