数组Diff,比较一维数组和二维数组的第二次元


Array Diff, compare 1d array to 2d arrays second dimension

array_diff( $files, $data[ ][0] ) 

是否有可能仅通过存储在[0]位置中的值来比较它们并迭代空的[]?


我的问题是我的$data数组是一个2d数组和$files是一个1d数组。$dataY坐标包含$filesX坐标。

这就是我要找的

// How I need array_diff to compare them. 
$files[0] -> $data[0][0]
$files[1] -> $data[1][0] // Y is constantly position 0, while both X's remain at the same position.

如果这是不可能的使发生使用预先制作的php函数,什么是最好的方法来解决这个问题?我真的不希望$data是一个2d数组,我所需要的只是将Y坐标值设置为x坐标值,并消除数组的额外维度。

我不确定这是不是你的意思,但你可以试试这个

$diffs = array_diff($files, array_map(function($a){ return $a[0]; }, $data));

另一种方式是:

$diffs = array();
foreach ($files as $key => $value) {
    if ($data[$key][0] != $value) {
        $diff[] = $files[$key]; // if you want to check the value of the files variable
        $diff[] = $data[$key][0]; // for the data variable
    }
}

我不知道性能的差异,但后者只适用于$files[$n] == $data[$n][0],所以如果我不得不猜测后者的性能会更快。