note 54979 added to ref.array
| From: | michaelatimreocom at osu1 dot php dot net | Date: | Thu, 21 Jul 2005 12:20:16 +0000 |
| Subject: | note 54979 added to ref.array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-92252@lists.php.net to get a copy of this message | ||
Reply to array_cartesian_product of skopek at mediatac dot com, 13-Oct-2004 12:44:
Your function does not work in my configuration (WinXP, apache 2.0, php 4.3.11).
This part of code:
...
} else { //if next returns false, then reset and go on with previuos array...
reset($arrays[$j]);
}
...
cause infinite loop.
Replacing to
...
} elseif (isset($arrays[$j])) {
reset($arrays[$j]);
}
...
works good.
My complete working function (additionaly strings as keys are allowed):
<?php
function array_cartesian_product($arrays)
{
$result = array();
$arrays = array_values($arrays);
$sizeIn = sizeof($arrays);
$size = $sizeIn > 0 ? 1 : 0;
foreach ($arrays as $array)
$size = $size * sizeof($array);
for ($i = 0; $i < $size; $i ++)
{
$result[$i] = array();
for ($j = 0; $j < $sizeIn; $j ++)
array_push($result[$i], current($arrays[$j]));
for ($j = ($sizeIn -1); $j >= 0; $j --)
{
if (next($arrays[$j]))
break;
elseif (isset ($arrays[$j]))
reset($arrays[$j]);
}
}
return $result;
}
?>
----
Manual Page -- http://www.php.net/manual/en/ref.array.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+54979
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+54979&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+54979&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+54979&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+54979&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+54979&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+54979&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+54979&report=yes
Search -- http://master.php.net/manage/user-notes.php