比较两个阵列,和array_diff [英] Comparing two arrays with array_diff
问题描述
我有以下的code,我试图比较两个阵列与和array_diff但是我一直没有得到结果。我不知道,如果它的问题,但也有许多领域数组中,我真的只是想比较1场......这可能吗?我缺少什么?
< PHP
$ JSON = file_get_contents(\"http://ebird.org/ws1.1/data/obs/region/recent?rtype=subnational1&r=US-AZ&back=7&fmt=json\");
$ json2 = file_get_contents(\"http://ebird.org/ws1.1/data/obs/region/recent?rtype=subnational1&r=US-NV&back=7&fmt=json\");$数组1 = json_de code($ JSON,TRUE);
$数组2 = json_de code($ json2,TRUE);如果($数组1 == $数组2){
回声没有区别;
}其他
后续代码var_dump(和array_diff($数组2,$数组1));
回声他们是不同的';?>
您将需要检查阵列针对对方:
$ Array_1 =阵列(1,2,3,4,5);
$ Array_2 =阵列(1,2,3,4,5,6);的print_r(和array_diff($ Array_1,$ Array_2));
将输出:
阵列
(
)
鉴于:
的print_r(和array_diff($ Array_2,$ Array_1));
将输出:
阵列
(
[5] => 6
)
所以这可能是一个解决方案:
函数ArrayDiff($ Array_1,$ Array_2){
$ Compare_1_To_2 =和array_diff($ Array_1,$ Array_2);
$ Compare_2_To_1 =和array_diff($ Array_2,$ Array_1);
$ Difference_Array = array_merge($ Compare_1_To_2,$ Compare_2_To_1);
返回$ Difference_Array;}的print_r(ArrayDiff($ Array_1,$ Array_2));
将输出:
阵列
(
[0] => 6
)
把这个变成一个if语句:
$ =差异ArrayDiff($ Array_2,$ Array_1);
如果(计数($差异)大于0){
回声有阵列之间的区别:';
的foreach($差异$不同){
回声< BR>中不同$。
}
所有的例子和code在开始基于该阵列($ Array_1和$ Array_2)
I have the following code and am trying to compare two array's with array_diff however I keep getting no results. I not sure if it matters, but there are many fields in the array and I really only want to compare 1 field...is this possible? what am I missing?
<?php
$json = file_get_contents("http://ebird.org/ws1.1/data/obs/region/recent?rtype=subnational1&r=US-AZ&back=7&fmt=json");
$json2 = file_get_contents("http://ebird.org/ws1.1/data/obs/region/recent?rtype=subnational1&r=US-NV&back=7&fmt=json");
$array1 = json_decode($json, TRUE);
$array2 = json_decode($json2, TRUE);
if ( $array1 == $array2 ) {
echo 'There are no differences';
}else
var_dump(array_diff($array2, $array1));
echo 'they are different';
?>
You will need to check the arrays against each other:
$Array_1 = array (1,2,3,4,5);
$Array_2 = array(1,2,3,4,5,6);
print_r(array_diff($Array_1,$Array_2));
Will output:
Array
(
)
Whereas:
print_r(array_diff($Array_2,$Array_1));
will output:
Array
(
[5] => 6
)
So this might be a solution:
function ArrayDiff ($Array_1, $Array_2){
$Compare_1_To_2 = array_diff($Array_1,$Array_2);
$Compare_2_To_1 = array_diff($Array_2,$Array_1);
$Difference_Array = array_merge($Compare_1_To_2,$Compare_2_To_1);
return $Difference_Array;
}
print_r(ArrayDiff($Array_1,$Array_2));
Which will output:
Array
(
[0] => 6
)
Putting this into an if statement:
$Differences = ArrayDiff($Array_2,$Array_1);
if (count($Differences) > 0){
echo 'There Are Differences Between The Array:';
foreach ($Differences AS $Different){
echo "<br>".$Different;
}
All the examples and code is based off the arrays at the start ($Array_1 and $Array_2)
这篇关于比较两个阵列,和array_diff的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!