Jump to content

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


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...

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.