【发布时间】: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循环,我建议你阅读更多关于循环的内容