和array_diff在PHP关联数组的数组 [英] array_diff on array of associative arrays in php
问题描述
我有形式的两个阵列
数组1:结果
结果
[0] =>阵列([名称] => foo的[ID] => 12)结果
[1] =>阵列([名称] =>酒吧[ID] => 34) ARRAY2:结果
[0] =>阵列([名称] =>酒吧[ID] => 34)结果
[1] =>阵列([名称] =>巴兹[ID] => 56)
的阵列来自数据库和任何两个对可以具有相同的名称,但ID是唯一的。我试图通过ID喜欢这样来比较数组:
The arrays come from a database and any two pairs can have the same name but ID's are unique. I am trying to compare the arrays by ID likes so:
$ one_not_two =和array_diff($数组1 [ID],$数组2 [ID]);
结果
但是,这并不返回任何东西。我也试过结果 $ one_not_two =和array_diff($数组1 [ID],$数组2 [ID]);
结果
它返回一个错误的说法是不是数组。本来我周围有通过提取ID添加到一个一维数组,只是比较这些,但现在一个新的功能要求我比较对。有什么建议?
$one_not_two = array_diff($array1[id], $array2[id]);
but that does not return anything. I also tried
$one_not_two = array_diff($array1[id], $array2[id]);
which returned an error "argument is not an array." Originally I got around it by extracting the IDs into a one-dimensional array and just comparing those but now a new feature requires me to compare the pairs. Any advice?
PS我们的服务器上运行PHP 5.3,如果有什么差别。
PS Our servers are running php 5.3 if that makes any difference.
推荐答案
由于阵列是多方面的,你必须提取IDS是这样的:
Because the arrays are multidimensional you have to extract the ids like this:
$ids1 = array();
foreach($array1 as $elem1)
$ids1[] = $elem1['id'];
$ids2 = array();
foreach($array2 as $elem2)
$ids2[] = $elem2['id'];
$one_not_two = array_diff($ids1,$ids2);
有关您的具体问题,请查看<一个href=\"http://stackoverflow.com/questions/11821680/array-diff-with-multidimensional-arrays\">array_diff()与多维数组
For your specific question, check out array_diff() with multidimensional arrays
这篇关于和array_diff在PHP关联数组的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!