From: e dot sand at elisand dot com Operating system: Irrelevant PHP version: 5.2.9 PHP Bug Type: Feature/Change Request Bug description: Allow foreach to iterate multiple arrays/objects similar to for syntax
Description: ------------ Using the for construct, you are able to define/utilize multiple variables: for ($i = 0, $j = 10; $i < 10; $i++, $j--) {...} I would like to propose that a similar syntax be allowed for the foreach construct which would allow iterating over multiple arrays/objects at once: foreach ($array1 as $i => $a, $array2 as $j => $b) {...} Such syntax and use would be useful when you have multiple arrays that are related, such as may be found with $_POST data when submitting arrays of values. For example, a form of inventory data where an id, name and quantity may be submitted - all in individual arrays but the indexes/keys would relate across arrays (id[0] -> name[0] -> quantity[0]). In such an example, you could write a simple foreach iterator as: foreach ($_POST['id'] as $key => $id, $_POST['name] as $name, $_POST['quantity'] as $qty) {...} This would save having to traditionally define $name and $qty within the foreach loop like so: $name = $_POST['name'][$key]; $qty = $_POST['quantity'][$key]; That is just a simple example however. As the proposed changes would allow iterating over multiple arrays at once, it could indeed save considerable lines of code. The only issues to debate with regards to this proposition is what happens if not all arrays being iterated have the same number of members. The other issue is the syntax, should http://bugs.php.net/bug.php?id=44530&edit=2 be considered - perhaps using ; instead of , to separate the arrays would be simple enough. -- Edit bug report at http://bugs.php.net/?id=47933&edit=1 -- Try a CVS snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=47933&r=trysnapshot52 Try a CVS snapshot (PHP 5.3): http://bugs.php.net/fix.php?id=47933&r=trysnapshot53 Try a CVS snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=47933&r=trysnapshot60 Fixed in CVS: http://bugs.php.net/fix.php?id=47933&r=fixedcvs Fixed in CVS and need be documented: http://bugs.php.net/fix.php?id=47933&r=needdocs Fixed in release: http://bugs.php.net/fix.php?id=47933&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=47933&r=needtrace Need Reproduce Script: http://bugs.php.net/fix.php?id=47933&r=needscript Try newer version: http://bugs.php.net/fix.php?id=47933&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=47933&r=support Expected behavior: http://bugs.php.net/fix.php?id=47933&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=47933&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=47933&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=47933&r=globals PHP 4 support discontinued: http://bugs.php.net/fix.php?id=47933&r=php4 Daylight Savings: http://bugs.php.net/fix.php?id=47933&r=dst IIS Stability: http://bugs.php.net/fix.php?id=47933&r=isapi Install GNU Sed: http://bugs.php.net/fix.php?id=47933&r=gnused Floating point limitations: http://bugs.php.net/fix.php?id=47933&r=float No Zend Extensions: http://bugs.php.net/fix.php?id=47933&r=nozend MySQL Configuration Error: http://bugs.php.net/fix.php?id=47933&r=mysqlcfg