Andris
Andris

Reputation: 1442

Php how to include in a variable name another variable

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.

Upvotes: 0

Views: 111

Answers (2)

Marti Markov
Marti Markov

Reputation: 766

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:

Upvotes: 1

Kanishka Panamaldeniya
Kanishka Panamaldeniya

Reputation: 17576

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
}

Upvotes: 1

Related Questions