Ok ... I will show example: $var1 = 'test'; $var2 = '1'; $var3 = '2'; Those 3 vars should be combined to get smth like this: $together = test1['2']; This is used in foreach to generate a proper form. It is a call for array. foreach ($together as $key => $value)... I made it this way: $together = $var1.$var2[$var3]; It didn't work in call for array... I've tried if all vars are properly set: print $var1.$var2.'['.var3.']' Result: test1['2']...
Here's one way to do it: $var1='test'; $var2=1; $var3=2; $together=array($var1, $var2, $var3); foreach ($together as $key => $value) { echo "{$key} = {$value} <br />"; } PHP:
I wanted to post this way too: $together=array(); $together['var1']='test 1'; $together['var2']='test 2'; $together['var3']='test 3'; foreach ($together as $key => $value) { echo "{$key} = {$value} <br />"; } PHP: Does that help?
No no ... the result of this example would be array... What I want to do is that this string would call a specific location of array ... First var (var1) is first part of array's name, second var (var2) is second part of array's name and var3 is number that calls part of this array. array.php <?php $test1 = array ( 1 => 'smth1', 2 => 'smth2' ); $test2 = array ( 1 => 'smthelse1', 2 => 'smthelse2' ); ?> myscript.php <?php include('array.php'); $var1 = 'test'; $var2 = '1'; //might be 2, 3, 4 ... $var3 = '2'; //might be 1,2,3,4 ... //let's say we want to call $test1['2'] ... var1, var2 and var3 are beeing changed depande on $_GET ... $together = $var1.$var2[$var3]; //together= test1[2] if we take a look or vars above... ?> But when I call together in for each (to call my array) I get error... I've tried to print $together... I can see only first part (var1) ... Then I've tried to print one by one print $var1; print $var2; print $var3; The result is test12... So variables are set properly... I guess there is a mistake when I make $together... Once again: var1, var2 & var3 are used to combain an ID to call an array. Var1 and Var2 are used for array's name, var3 is used to get part of array.
How should I solve it... The problem is ... when I print $together; I get value from first value ($var1) only...
If I understand your question correctly you need to use eval. Something like eval("\$together = \$$var1$var2[$var3]"); so if: $var1="test'; $var2="1"; $var3="2" you'd get $together = $test1[2];
<?php /* IMO this is a much more efficient way to store your data */ $test = array ( "1" => array("smith0", "smith1", "smith2"), "2" => array("jones0", "jones1", "jones2") ); /* Set from $_GET right ? */ $var1 = 'test'; $var2 = '1'; //might be 2, 3, 4 ... $var3 = '2'; //might be 1,2,3,4 ... /* Just so you can see how the array is structured now */ echo "<pre>"; print_r($test); echo "</pre>"; /* calling eval on the vars will make $string available in the script, it does not echo content */ eval ("\$string = $". $var1 ."[".$var2."][".$var3."];"); // see .... echo $string; ?> PHP:
Well thanks for your help... I had this problem ... I had to call $test1 = array (...); $test2 = array (...); etc. with three generated variables... var1 = test var2 = 1,2,3... etc var3 = 1,2,3... etc Than I've changed structure to: $test = array( '1' => array(...), '2' => array(...),... ); So I used my variables: $var1[$var2][$var3] and it worked perfectly! Thanks for all your help!