@@ -692,43 +692,39 @@ public function cartesianProduct(callable $operation = null, callable $filter =
692
692
return new Collection ([]);
693
693
}
694
694
695
- // contains each array of the collection
696
- $ values = [];
697
- // contains the keys of each array
698
- $ valuesKeys = [];
699
- // contains the number of elements of each array
700
- $ counts = [];
695
+ $ collectionArrays = [];
696
+ $ collectionArraysKeys = [];
697
+ $ collectionArraysCounts = [];
701
698
702
699
foreach ($ this as $ value ) {
703
- // fail if any of the values is a multidimensional array
704
700
if (count ($ value ) !== count ($ value , COUNT_RECURSIVE )) {
705
701
throw new LogicException ('Cannot find the cartesian product of a multidimensional array ' );
706
702
}
707
703
708
- $ valuesKeys [] = array_keys ($ value );
709
- $ counts [] = count ($ value );
710
- $ values [] = $ value ;
704
+ $ collectionArraysKeys [] = array_keys ($ value );
705
+ $ collectionArraysCounts [] = count ($ value );
706
+ $ collectionArrays [] = $ value ;
711
707
}
712
708
713
709
$ result = [];
714
- $ lastIndex = count ($ values ) - 1 ;
710
+ $ lastIndex = count ($ collectionArrays ) - 1 ;
715
711
// holds the indexes of the arrays that generate the current combination
716
712
$ currentIndexes = array_fill (0 , $ lastIndex + 1 , 0 );
717
713
718
714
$ changeIndex = $ lastIndex ;
719
715
720
- while (!($ changeIndex === 0 && $ currentIndexes [0 ] === $ counts [0 ])) {
716
+ while (!($ changeIndex === 0 && $ currentIndexes [0 ] === $ collectionArraysCounts [0 ])) {
721
717
$ currentCombination = array_map (function ($ value , $ keys , $ index ) {
722
718
return $ value [$ keys [$ index ]];
723
- }, $ values , $ valuesKeys , $ currentIndexes );
719
+ }, $ collectionArrays , $ collectionArraysKeys , $ currentIndexes );
724
720
725
721
if ($ filter === null || call_user_func_array ($ filter , $ currentCombination )) {
726
722
$ result [] = ($ operation === null ) ? $ currentCombination : call_user_func_array ($ operation , $ currentCombination );
727
723
}
728
724
729
725
$ currentIndexes [$ lastIndex ]++;
730
726
731
- for ($ changeIndex = $ lastIndex ; $ currentIndexes [$ changeIndex ] === $ counts [$ changeIndex ] && $ changeIndex > 0 ; $ changeIndex --) {
727
+ for ($ changeIndex = $ lastIndex ; $ currentIndexes [$ changeIndex ] === $ collectionArraysCounts [$ changeIndex ] && $ changeIndex > 0 ; $ changeIndex --) {
732
728
$ currentIndexes [$ changeIndex ] = 0 ;
733
729
$ currentIndexes [$ changeIndex - 1 ]++;
734
730
}
0 commit comments