For*_*low 0 php arrays array-intersect multidimensional-array
我正在尝试array_intersect比较两个数组数组.
$start[]=array(
'id'=>1,
'name'=>'Up',
'action'=>'up'
);
$start[]=array(
'id'=>3,
'name'=>'Down',
'action'=>'down'
);
$start[]=array(
'id'=>5,
'name'=>'Left',
'action'=>'left'
);
$end[]=array(
'id'=>1,
'name'=>'Up',
'action'=>'up'
);
$end[]=array(
'id'=>9,
'name'=>'Up',
'action'=>'up'
);
$result=array_intersect($start,$end);
Run Code Online (Sandbox Code Playgroud)
但是,我总是收到通知消息:
注意:xyz行上的testfile.php中的数组转换为字符串
而且这种比较实际上并没有发生.
比较两个阵列而不重新发明轮子或达到过于复杂的东西的最佳方法是什么?
小智 6
array_diff和array_intersect将主数组中的每个元素转换为字符串以进行比较.如果您想要进行不同的比较,那么您可以将回调方法与以下内置函数一起使用:
array_uintersect_assoc() - Computes the intersection of arrays with additional index check, compares data by a callback function
array_intersect_uassoc() - Computes the intersection of arrays with additional index check, compares indexes by a callback function
array_uintersect_uassoc() - Computes the intersection of arrays with additional index check, compares data and indexes by a callback functions
Run Code Online (Sandbox Code Playgroud)
我通过在PHP.net中搜索函数array_diff找到了这些,并按照相关的函数链接.这是看待做事的替代方案的好方法.
| 归档时间: |
|
| 查看次数: |
5865 次 |
| 最近记录: |