简体   繁体   English

从多维数组获取数组中的相交值

[英]Get intersect values in a array from Multi-dimensional array

I have this array 我有这个数组

$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);

The subarrays in the main array can be increase, for now there are 3 sub arrays. 主数组中的子数组可以增加,目前有3个子数组。

How can I get just intersect values as array from the main array. 如何从主数组中获取仅作为数组的相交值。

For example, "Monaco Swarovski", "Long" value should not be on intersect array, because: these values are not exist in all sub arrays. 例如,“ Monaco Swarovski”,“ Long”值不应该在相交数组上,因为:这些值并非在所有子数组中都存在。

The output array (intersect) structure must like a sub array on main array format. 输出数组(相交)结构必须类似于主数组格式的子数组。

For this main array, the output should be this: 对于此主数组,输出应为:

$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);

So, just intersect values in same sub array structure. 因此,只需在相同的子数组结构中相交值即可。

I tried so many methods on stackoverflow, but some of them were only working for two sub arrays, some of them did not give exactly the right intersection. 我在stackoverflow上尝试了很多方法,但是其中一些仅适用于两个子数组,其中一些没有给出正确的交集。

I think we need to use intersect functions correctly for the solution. 我认为我们需要为解决方案正确使用相交函数。

Thanks for your helps. 感谢您的帮助。

Gathering the corresponding sub-sub-arrays for all sub-arrays in a temporary array, and using the spread operator to feed that to array_intersect, you could do it like this: 收集临时数组中所有子数组的相应子子数组,并使用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);

https://3v4l.org/0OcbN https://3v4l.org/0OcbN

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM