我有这个数组
$data = json_decode('[
[
["Monaco Chain"],
["Monaco Diamond Cut","Monaco Plain","Monaco Swarovski"],
["11.50 mm","13.50 mm","15.50 mm","17.50 mm","6.50 mm","8.00 mm","9.50 mm"],
["18.00","20.00","22.00","24.00","26.00","28.00","30.00","7.00","7.50","7.75","8.00","8.25","8.50","9.00","7.25","8.75","9.50","16.00","9.25"],
["ROSE","WHITE","YELLOW"],
["10","14","18","21"],
["New","Long"],
["No","Yes","N/A"],
["No","Yes","N/A"]
],
[
["Monaco Chain"],
["Monaco Diamond Cut","Monaco Plain"],
["6.50 mm"],
["16.00","18.00","20.00","22.00","24.00","26.00","28.00","30.00","7.50","8.00","9.00"],
["ROSE","WHITE","YELLOW"],
["10","14","18","21"],
["New"],
["No","Yes","N/A"],
["No","Yes","N/A"]
],[
["Monaco Chain"],
["Monaco Diamond Cut","Monaco Swarovski"],
["11.50 mm","13.50 mm","15.50 mm","17.50 mm","6.50 mm","8.00 mm","9.50 mm"],
["18.00","20.00","22.00","24.00","26.00","28.00","30.00","7.00","7.50","7.75","8.00","8.25","8.50","9.00","7.25","8.75","9.50","16.00","9.25"],
["ROSE","WHITE","YELLOW"],
["10","14","18","21"],
["New","Long"],
["No"],
["No","Yes"]
]
]
', true);
可以增加主数组中的子数组,目前有3个子数组。
如何从主数组中获取与数组相交的值。
例如,“ Monaco Swarovski”,“ Long”值不应该在相交数组上,因为:这些值并非在所有子数组中都存在。
输出数组(相交)结构必须像主数组格式的子数组一样。
对于此主数组,输出应为:
$data = json_decode('
[
[
["Monaco Chain"],
["Monaco Diamond Cut"],
["6.50 mm"],
["18.00","20.00","22.00","24.00","26.00","28.00","30.00","7.50","8.00","9.00"],
["ROSE","WHITE","YELLOW"],
["10","14","18","21"],
["New"],
["No"],
["No","Yes"]
]
]', true);
因此,只需将相同子数组结构中的值相交即可。
我在stackoverflow上尝试了很多方法,但是其中一些仅适用于两个子数组,其中一些没有给出正确的交集。
我认为我们需要正确使用相交函数作为解决方案。
感谢您的帮助。
答案 0 :(得分:0)
为临时数组中的所有子数组收集相应的子子数组,并使用spread运算符将其馈送到array_intersect,您可以像这样进行操作:
$subArrayCount = count($data);
$subSubArrayCount = count($data[0]);
$result = [];
for($i=0; $i<$subSubArrayCount; ++$i) {
$temp = [];
for($j=0; $j<$subArrayCount; ++$j) {
$temp[] = $data[$j][$i];
}
$result[] = array_intersect(...$temp);
}
var_dump($result);