我想比较两个不同的数组。我正在寻找array1中出现但不出现在array2中的值。我一直在撞墙,因为它似乎很简单但不起作用。
我试图找到数组的键然后我要搜索它们
$orgKey;
foreach($org as $k => $v){
$orgKey[$k][] = $v['title'];
}
$newKey;
foreach($new as $k => $v){
$newKey[$k][] = $v['title'];
}
echo "orgkey";
echo print_r($orgKey);
echo "newkey<br/>";
echo print_r($newKey);
$e = array_diff($orgKey,$newKey);
echo print_r($e);
但每次都给我一个空数组。
我也在foreach中尝试了foreach,并意识到我无法找到如何搜索不存在的值,因为foreach循环本身没有重复。
foreach($org as $k => $v){
foreach($new as $a => $b){
$count = count($new);
$i = 0;
if($v['title'] == $b['title']){
//This is where i realize i will find duplicates of values that don't exist
}
}
}
我的阵列。 $组织
[0] => Array
(
[title] => Ahiris Angels
[r_id] => 1276
[t_id] => 1277
[name] =>
[column1] =>
[column2] =>
)
[1] => Array
(
[title] => Alistars Stars
[r_id] => 1276
[t_id] => 1278
[name] =>
[column1] =>
[column2] =>
)
[2] => Array
(
[title] => Ammumu
[r_id] => 1276
[t_id] => 1279
[name] =>
[column1] =>
[column2] =>
)
$ new数组可以是任何值,但保留$ org数组的结构。
我的目标是拥有一个类似这个$ org的数组
[0] => Array
(
[title] => Ahiris Angels
[r_id] => 1276
[t_id] => 1277
[name] =>
[column1] =>
[column2] =>
)
[1] => Array
(
[title] => Alistars Stars
[r_id] => 1276
[t_id] => 1278
[name] =>
[column1] =>
[column2] =>
)
将其与$ new
进行比较[0] => Array
(
[title] => Ahiris Angels
[r_id] => 1276
[t_id] => 1277
[name] =>
[column1] =>
[column2] =>
)
我的结果将是
[1] => Array
(
[title] => Alistars Stars
[r_id] => 1276
[t_id] => 1278
[name] =>
[column1] =>
[column2] =>
)
答案 0 :(得分:0)
这应该做你想做的事情:
foreach ($org as $v)
{
$orgKey[$v['title']] = $v;
}
foreach ($new as $v)
{
$newKey[$v['title']] = $v;
}
$e = array_diff_key($orgKey, $newKey);
$e = array_values($e);
对array_values
的最后一次通话是可选的;它只是将顺序整数重新分配给键而不是留在标题中。