[英]Build a unique string list from multidimensional array items
I have a multidimensional array:我有一个多维数组:
$items = array(
array("A1", "A2", "A3"),
array("B1","B2","B3","B4"),
array("C1","C2","C3","C4","C5"),
array("D1","D2","D3"));
I need to build a list of unique strings, and each should contain one item from each array, like this:我需要构建一个唯一字符串列表,每个字符串都应该包含每个数组中的一个项目,如下所示:
A1 B1 C1 D1 , A1 B1 C1 D1 ,
A1 B1 C1 D2 , A1 B1 C1 D2 ,
A1 B1 C1 D3 , A1 B1 C1 D3 ,
A1 B1 C2 D1 , A1 B1 C2 D1 ,
A1 B1 C2 D2 , A1 B1 C2 D2 ,
etc...等等...
Example data above should produce 180 combinations (3*4*5*3=180).上面的示例数据应该产生 180 种组合(3*4*5*3=180)。
The difficulty is that the code (in best case) should work for any number of arrays.困难在于代码(在最好的情况下)应该适用于任意数量的数组。 Not just for 4 as in the example above.
不仅仅是上面例子中的 4。
For example, I would add: array("E1", "E2");
例如,我会添加:
array("E1", "E2");
and code should still work.并且代码应该仍然有效。
A simpler version might be working with fixed set of arrays, for example 4 (as in the example above).更简单的版本可能使用固定的数组集,例如 4(如上例所示)。
Of course I can use for
loops, but in this case it will be required to add another for
loop, each time I add another array of data (not so universal, right?).当然我可以使用
for
循环,但在这种情况下,每次我添加另一个数据数组时,都需要添加另一个for
循环(不是那么通用,对吧?)。
Here's what I tried, but it didn't work out:这是我尝试过的,但没有成功:
$items = array(
array("A1", "A2", "A3"),
array("B1","B2","B3","B4"),
array("C1","C2","C3","C4","C5"),
array("D1","D2","D3"));
$depth = count($items);
$index = $depth - 1;
for ($i = 0; $i <= $index; $i++) {
$depth2 = count($items[$i]);
$index2 = $depth2 - 1;
echo "SET: ";
for ($i2 = 0; $i2 <= $index2; $i2++) {
echo $items[$i2][$i].", ";
}
echo "<br>";
}
Expected result is the list of 180 unique strings, like:预期结果是 180 个唯一字符串的列表,例如:
A1 B1 C1 D1 , A1 B1 C1 D1 ,
A1 B1 C1 D2 , A1 B1 C1 D2 ,
A1 B1 C1 D3 , A1 B1 C1 D3 ,
A1 B1 C2 D1 , A1 B1 C2 D1 ,
A1 B1 C2 D2 , A1 B1 C2 D2 ,
etc...等等...
You can use recursion to build the result set similar to the way you might permute an array with backtracking.您可以使用递归来构建结果集,类似于使用回溯置换数组的方式。 The idea is to include each item one at a time from each array and add it to the in-progress result, then recurse on that possibility.
这个想法是从每个数组中一次包含一个项目,并将其添加到进行中的结果中,然后对该可能性进行递归。 After exploring all of the result possibilities for a given element, we discard it from further consideration and move on to the next element, repeating the process.
在探索了给定元素的所有结果可能性之后,我们将其从进一步考虑中丢弃,并继续下一个元素,重复该过程。
<?php
function combos($items, &$curr=[], &$res=[], $i=0) {
if ($i >= count($items)) {
$res[]= $curr;
}
else {
foreach ($items[$i] as $e) {
$curr[]= $e;
combos($items, $curr, $res, $i + 1);
array_pop($curr);
}
}
return $res;
}
$items = [
["A1", "A2", "A3"],
["B1","B2","B3","B4"],
["C1","C2","C3","C4","C5"],
["D1","D2","D3"]
];
$i = 0;
foreach (combos($items) as $combo) {
echo str_pad($i++ . ": ", 5, " ", false) . implode(" ", $combo) . "\n";
}
Output:输出:
0: A1 B1 C1 D1
1: A1 B1 C1 D2
2: A1 B1 C1 D3
3: A1 B1 C2 D1
4: A1 B1 C2 D2
5: A1 B1 C2 D3
6: A1 B1 C3 D1
7: A1 B1 C3 D2
...
172: A3 B4 C3 D2
173: A3 B4 C3 D3
174: A3 B4 C4 D1
175: A3 B4 C4 D2
176: A3 B4 C4 D3
177: A3 B4 C5 D1
178: A3 B4 C5 D2
179: A3 B4 C5 D3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.