数组差异,将一维数组与二维二维数组进行比较

安德鲁
array_diff( $files, $data[ ][0] ) 

是否可以仅通过存储在[0]位置内的值进行比较,然后遍历空的[]来进行比较?


我的问题是我的$data数组是2d数组,$files是1d数组。Y坐标$data包含的X坐标$files

这本质上就是我要寻找的

// 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]在我不得不猜测后一个性能会更快的情况下,后者才有效。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章