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.

Claire
  • 3,383
  • 11
  • 41
  • 72

6 Answers6

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;
}
Sampson
  • 251,934
  • 70
  • 517
  • 549
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.

Mike Graf
  • 4,589
  • 2
  • 38
  • 55
0
for ($i =1;$i<4;$i++){
    $var = 'item'.$i;
    echo $$var;
}

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

Ibu
  • 39,552
  • 10
  • 71
  • 99
0

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

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

Samy Dindane
  • 15,488
  • 3
  • 36
  • 49
  • 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 – Claire May 16 '12 at 16:31
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"
Brad
  • 13,946
  • 10
  • 56
  • 71
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
?>
maiorano84
  • 10,680
  • 3
  • 31
  • 46