note 111825 added to control-structures.foreach
| From: | Ashus | Date: | Tue, 02 Apr 2013 13:25:11 +0000 |
| Subject: | note 111825 added to control-structures.foreach | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-194291@lists.php.net to get a copy of this message | ||
If you wondered how to create a list of all possible combinations of variable amount of arrays
(multiple foreach), you might use this:
<?php
$a[0] = array('a1','a2');
$a[1] = array('b1','b2','b3');
$a[2] = array('c1','c2');
function getAllCombinations($a,$i,$s)
{
foreach ($a[$i] as $v)
{
if (!isset($a[$i+1]))
{
echo $s.$v."\n";
} else {
getAllCombinations($a,$i+1,$s.$v);
}
}
return $s;
}
echo getAllCombinations($a,0,'');
?>
the result:
a1b1c1
a1b1c2
a1b2c1
a1b2c2
a1b3c1
a1b3c2
a2b1c1
a2b1c2
a2b2c1
a2b2c2
a2b3c1
a2b3c2
----
Server IP: 69.147.83.199
Probable Submitter: 82.209.8.34
----
Manual Page -- http://php.net/manual/en/control-structures.foreach.php
Edit -- https://master.php.net/note/edit/111825
Del: integrated -- https://master.php.net/note/delete/111825/integrated
Del: useless -- https://master.php.net/note/delete/111825/useless
Del: bad code -- https://master.php.net/note/delete/111825/bad+code
Del: spam -- https://master.php.net/note/delete/111825/spam
Del: non-english -- https://master.php.net/note/delete/111825/non-english
Del: in docs -- https://master.php.net/note/delete/111825/in+docs
Del: other reasons-- https://master.php.net/note/delete/111825
Reject -- https://master.php.net/note/reject/111825
Search -- https://master.php.net/manage/user-notes.php