note 54979 deleted from ref.array by levim

From: Date: Tue, 06 May 2014 22:00:19 +0000
Subject: note 54979 deleted from ref.array by levim
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-199436@lists.php.net to get a copy of this message
Note Submitter: michael at imreo com ---- 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; } ?>

« previous php.notes (#199436) next »