TQ_designs Posted August 16, 2009 Share Posted August 16, 2009 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... Link to comment https://forums.phpfreaks.com/topic/170525-using-variable-variables-and-to-change-array-name/ Share on other sites More sharing options...
ignace Posted August 16, 2009 Share Posted August 16, 2009 echo current($arr$i); //I know this is illegal syntax - need proper way to do the same thing $arrayName = 'arr' . $i; $array = ${$arrayName}; echo current($array); next($array); Link to comment https://forums.phpfreaks.com/topic/170525-using-variable-variables-and-to-change-array-name/#findComment-899527 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.