0

I want to access a variable that is either called $item1, $item2 or $item3.

I want to access this variable inside a for loop where $i is ++ every time. using $item.$i or something similar. However using that code means that I am trying to join the contents of two variables, and there is no variable called $item.

8
  • 3
    Why don't you use an array of values? Commented May 16, 2012 at 16:28
  • 3
    php.net/manual/en/language.variables.variable.php Commented May 16, 2012 at 16:29
  • 2
    How to write unmaintable code. Commented May 16, 2012 at 16:32
  • 2
    Using multidimensional arrays is totally normal ;) Variable variables is really a bad and inflexible approach. Commented May 16, 2012 at 16:38
  • 3
    Just because you can, doesnt mean you should. try to avoid variable variables if possible for readability's sake. Commented May 16, 2012 at 16:38

6 Answers 6

4

Arrays: A Better Method

While PHP does permit you to build dynamic variable names from various other values, you probably shouldn't in this case. It seems to me that an array would be more appropriate for you:

$items = array( 0, 12, 34 );

You could then access each value individually:

echo $items[0]; // 0
echo $items[1]; // 12

Or loop over the entire set:

foreach ( $items as $number ) {
  echo $number; // 1st: 0, 2nd: 12, 3rd: 34
}

Merging Multiple Arrays

You indicated in the comments on the OP that $item1 through $item3 are already arrays. You could merge them all together into one array if you like with array_merge(), demonstrated below:

$item1 = array( 1, 2 );
$item2 = array( 3, 4 );
$item3 = array( 5, 6 );

$newAr = array_merge( $item1, $item2, $item3 );

print_r( $newAr );

Which outputs:

Array
(
  [0] => 1
  [1] => 2
  [2] => 3
  [3] => 4
  [4] => 5
  [5] => 6
)

If You Must: Dynamic Variable Names

For completeness, if you were to solve your problem by dynamically constructing variable names, you could do the following:

$item1 = 12;
$item2 = 23;
$item3 = 42;

for ( $i = 1; $i <= 3; $i++ ) {
  echo ${"item".$i} . PHP_EOL;
}
Sign up to request clarification or add additional context in comments.

2 Comments

array_merge, didn't know about that one thanks
With array_merge be careful of key collisions (for string keys). see: php.net/array_merge
2

build the variable name you want to access into another variable then use the variable variable syntax

<?php
   $item1 = 'a';
   $item2 = 'b';
   $item3 = 'c';

   for ($i = 1; $i<=3; $i++) {
       $varname = 'item' . $i;
       echo $$varname;
   }
?>

output:

abc

Note there are other ways to do this, see the manual.

2 Comments

Finally something approaching a correct answer.
Just noticed this answer after I posted mine. Eerie how we thought of the same variable values.
0

Use ${'item'.$i}
If $i == 1, then you will access $item1.

But it's better to use arrays in your case.

1 Comment

thanks, I'm looking into doing that now. You see $item1, 2 and 3 are already arrays, so it would need to be a multidimensional array
0
for ($i =1;$i<4;$i++){
    $var = 'item'.$i;
    echo $$var;
}

Here you are using the the double $ to create a variable variable.

3 Comments

Too many $s (and I'm not talking about the double one ;-) )
@DaveRandom it's kinda hard not to use $ in php
Run this code and you will find it does not work. Look at the line where you assign a value to $var.
0

Can you use an array instead of individual variables? then you can reference array elements by index value based in i.

$items = array();

$i = 1;
$items[$i] = "foo";

$i++;
$items[$i] = "bah";

echo $items[1], $items[2];     // gives "foobah"

2 Comments

You seem to be in Javascript mode, i think...
D'oh! thanks Dave. Who's says polyglot is the way forward?
0

It's a little late, and the accepted answer is the proper way to do this, but PHP does allow you to access variable variable names in the way OP describes:

<?php
$item1 = 'a';
$item2 = 'b';
$item3 = 'c';
for($i=1;$i<=3;$i++)
    echo ${"item$i"}; //Outputs: abc
?>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.