Jump to content

Using variable variables ($ and $$) to change array name?


TQ_designs

Recommended Posts

This is something that seems like it should be simple, but I can't hammer out the proper syntax :( Let's say you have a few arrays coming through from a form, like this:

 

$arr0 = $_POST['arr0'];
$arr1 = $_POST['arr1'];
$arr2 = $_POST['arr2'];
$arr3 = $_POST['arr3'];

 

I need to put them in the foreach loop of a different array, something like this:

 

foreach ($otherArray as $otherArrayValue) {
   for ($i=0;$i<4;$i++) {
      echo $otherArrayValue;
      echo current($arr$i); //I know this is illegal syntax - need proper way to do the same thing
      next($arr$i);
   }
}

 

In other words, I want to use the counter $i to iterate through arrays $arr0, $arr1, etc. Note that it's not the array values I want to change, just the name of the array being called - can I use the method described here - http://ca.php.net/language.variables.variable - for this purpose? If so, how? My attempts so far haven't worked out...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.