【问题标题】:PHP - Combine multiple arrays with each otherPHP - 将多个数组相互组合
【发布时间】:2020-11-19 23:21:45
【问题描述】:

我一直在研究一种将包含属性的数组相互组合的工具。

我遇到的问题是我不知道如何实现这个目标。

我首先使用以下代码将 JSON 解码为数组:

$input = '{"1":[["Red"],["Green"],["Blue"],["Purple"]],"2":[["S"],["M"],["L"]],"3":[["Wool"],["Cotton"]]}';
$arr = json_decode($input, true);
print_r($arr);

从那时起,我不知道该怎么办。我一直在尝试遍历数组并合并数组,但我找不到任何方法来达到我的目标。

数组中的以下输出是我的目标:

Red,S,Wool
Red,S,Cotton
Red,M,Wool
Red,M,Cotton
Red,L,Wool
Red,L,Cotton
Green,S,Wool
Green,S,Cotton
Green,M,Wool
Green,M,Cotton
Green,L,Wool
Green,L,Cotton
Blue,S,Wool
Blue,S,Cotton
Blue,M,Wool
Blue,M,Cotton
Blue,L,Wool
Blue,L,Cotton
Purple,S,Wool
Purple,S,Cotton
Purple,M,Wool
Purple,M,Cotton
Purple,L,Wool
Purple,L,Cotton

还有可能会有 3 个而不是 3 个,比如 4 个、5 个或更多具有属性的数组。

我找到了一个计算这个的工具,但不清楚幕后代码的作用。
https://www.dcode.fr/choices-combinations

是否有人可以向我提供解决此解决方案的代码或将我链接到显示如何解决的现有问题。

【问题讨论】:

  • 这似乎与您正在寻找的内容相似:gist.github.com/rxnlabs/…
  • 这只是一个for循环,我建议你阅读更多关于循环的内容

标签: php arrays arraylist


【解决方案1】:

您可以使用递归函数来创建每个不同子数组的所有组合。例如:

function combinations($array) {
    if (count($array) == 1) return reset($array);
    $result = array();
    foreach (reset($array) as $value) {
        foreach (combinations(array_slice($array, 1)) as $comb) {
            $result[] = array_merge($value, $comb);
        }
    }
    return $result;
}
$input = '{"1":[["Red"],["Green"],["Blue"],["Purple"]],"2":[["S"],["M"],["L"]],"3":[["Wool"],["Cotton"]]}';
$arr = json_decode($input, true);
$combs = combinations($arr);

这将返回一个这样的数组数组(基于您的示例输入数据):

Array
(
    [0] => Array
        (
            [0] => Red
            [1] => S
            [2] => Wool
        )
    [1] => Array
        (
            [0] => Red
            [1] => S
            [2] => Cotton
        )
    [2] => Array
        (
            [0] => Red
            [1] => M
            [2] => Wool
        )
    ...
    [22] => Array
        (
            [0] => Purple
            [1] => L
            [2] => Wool
        )
    [23] => Array
        (
            [0] => Purple
            [1] => L
            [2] => Cotton
        )
)

要获取逗号分隔的字符串列表,您可以使用array_mapimplode 应用于这些数组,例如

$csv = array_map(function ($a) { return implode(',', $a); }, $combs);

这将给出一个这样的数组:

Array
(
    [0] => Red,S,Wool
    [1] => Red,S,Cotton
    [2] => Red,M,Wool
    [3] => Red,M,Cotton
    [4] => Red,L,Wool
    [5] => Red,L,Cotton
    [6] => Green,S,Wool
    [7] => Green,S,Cotton
    [8] => Green,M,Wool
    [9] => Green,M,Cotton
    [10] => Green,L,Wool
    [11] => Green,L,Cotton
    [12] => Blue,S,Wool
    [13] => Blue,S,Cotton
    [14] => Blue,M,Wool
    [15] => Blue,M,Cotton
    [16] => Blue,L,Wool
    [17] => Blue,L,Cotton
    [18] => Purple,S,Wool
    [19] => Purple,S,Cotton
    [20] => Purple,M,Wool
    [21] => Purple,M,Cotton
    [22] => Purple,L,Wool
    [23] => Purple,L,Cotton
)

Demo on 3v4l.org

【讨论】:

  • 非常感谢您的宝贵时间,工作就像一个魅力!
  • @Joshua 不用担心 - 我很高兴能帮上忙。
【解决方案2】:

您的输入当前将所有颜色放入一个数组中,然后放入另一个数组中。 [["Red"],["Green"],["Blue"],["Purple"]]。理想情况下,您希望他们像["Red", "Green", "Blue", "Purple"]

除此之外,您还可以遍历元素将它们转换为所需的结构。 一旦你有了颜色、尺寸和材料的结构,你就可以循环它们并将它们添加到一个新的数组中。

$input = '{"1":[["Red"],["Green"],["Blue"],["Purple"]],"2":[["S"],["M"],["L"]],"3":[["Wool"],["Cotton"]]}';
$arr = json_decode($input, true);

$test = [];
$sizes = [];
$colours = [];
$materials = [];

foreach($arr[1] as $key => $colour) {
    $colours[] = $colour[0];    
}

foreach($arr[2] as $key => $size) {
    $sizes[] = $size[0];    
}

foreach($arr[3] as $key => $material) {
    $materials[] = $material[0];    
}

foreach ($colours as $key => $colour) {
    foreach($sizes as $key => $size) {
        foreach ($materials as $key => $material) {
            $test[] = $colour .','. $size . ','. $material;
        }
    }
}

print_r($test);

虽然理想情况下这不是最有效的,但它会达到您想要的输出。 Demo here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多