-1

我有以下代码,并试图将两个数组与 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");

$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';

?>
4

2 回答 2

3

您将需要相互检查数组:

$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));

将输出:

Array
(
)

然而:

 print_r(array_diff($Array_2,$Array_1));

将输出:

Array
(
    [5] => 6
)

所以这可能是一个解决方案:

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));

这将输出:

Array
(
    [0] => 6
)

将其放入 if 语句中:

$Differences = ArrayDiff($Array_2,$Array_1);
if (count($Differences) > 0){
    echo 'There Are Differences Between The Array:';
    foreach ($Differences AS $Different){
        echo "<br>".$Different;
    }

所有示例和代码都基于开头的数组($Array_1 和 $Array_2)

于 2013-04-09T17:49:27.177 回答
0
    $po_line_array=array();
    $po_line_clone_array=array();

    foreach($cart->line_items as $line_no => $po_line)
         $po_line_array[$line_no]=$po_line->labdip_details_id;
       print_r($po_line_array,1);
    foreach($cart->line_items_clone as $line_no_clone => $po_line_clone)
         $po_line_clone_array[$line_no_clone]=$po_line_clone->labdip_details_id;
       print_r($po_line_clone_array,1);

     $result=array_diff($po_line_clone_array,$po_line_array);
     print_r($result,1);

输出:

 Array ( [0] => 101 )

    Array ( [0] => 101 [1] => 103 )

    Array ( [1] => 103 )
于 2017-03-29T05:49:58.800 回答