简体   繁体   English

在php中比较两个数组

[英]comparing two arrays in php

I use this code : 我用这个代码:

 $new = array(
     "123" => "a",
     "456" => "b"
 );

 $old = array(
     "123" => "a",
     "456" => "b"
 );

then the $new array become like this: 然后$new数组变成这样:

 $new = array(
     "456" => "b",
     "123" => "c",
     "789" => "e"
 );

as you see the count of $new array increased and the order of elements changed and the value at key 123 also changed. 如您所见, $new数组的数量增加了,元素的顺序也改变了,键123的值也改变了。 I need to compare the $new array against the $old array and catch only the change made on the value at key 123 without caring about the order and the count of elements. 我需要将$new数组与$old数组进行比较,只捕获键123上的值所做的更改,而无需关心元素的顺序和计数。 I tried: 我试过了:

 $result = array_diff( $new, $old );
 print_r( $result );

output : 输出:

 Array ( [123] => c [789] => e )

UPDATE. UPDATE。 quite confusing. 相当混乱。 now I think we got it 现在我想我们明白了

$old = array(
    "123"    => "a",
    "456"    => "b"
);
$new = array(
    "456"    => "b",
    "123"    => "c", // catch this (element in old array that is changed)
    "789"    => "e"
);

$new2 = array();
foreach ($new as $key => $new_val)
{
    if (isset($old[$key])) // belongs to old array?
    {
        if ($old[$key] != $new_val) // has changed?
            $new2[$key] = $new[$key]; // catch it
    }
}

// output $new2:
array (
  123 => 'c',
)

You first of all want to have those elements of $new that are changed compared to $old (see array_diff_assoc ): 首先,您希望将$new元素与$old相比更改(请参阅array_diff_assoc ):

$changed = array_diff_assoc($new, $old);

Of that result you want to have only those elements that have their key in $old (see array_intersect_key ): 在那个结果中,你只想拥有那些在$old中具有键的元素(参见array_intersect_key ):

$result  = array_intersect_key($changed, $old);

And that's it. 就是这样。 You can wrap that into each other if it helps: 如果它有帮助,你可以将它包装在一起:

array_intersect_key(array_diff_assoc($new, $old), $old);

Result is: 结果是:

array(1) {
  [123] =>
  string(1) "c"
}

Full example ( Demo ): 完整示例( 演示 ):

$old = array(
    "123" => "a",
    "456" => "b"
);

$new = array(
    "456" => "b",
    "123" => "c", // catch only the change made on the value at key 123 
    "789" => "e"
);

$changed = array_diff_assoc($new, $old);
$result  = array_intersect_key($changed, $old);

var_dump($result);

Just a final note: There are many array functions in PHP. 最后一点:PHP中有许多数组函数。 It's worth to go through the list and look what is fitting because most often you only need one or two of them to get things like these done. 值得仔细阅读列表,看看哪些是合适的,因为大多数情况下你只需要其中的一两个来完成这些事情。

You use this code for your requirements 您可以根据需要使用此代码

 <?php
    function key_compare_func($key1, $key2)
    {
        if ($key1 == $key2)
            return 0;
        else if ($key1 > $key2)
            return 1;
        else
            return -1;
    }

    $array1 = array('blue'  => 1, 'red'  => 2, 'green'  => 3, 'purple' => 4);
    $array2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan'   => 8);

    var_dump(array_intersect_ukey($array1, $array2, 'key_compare_func'));
    ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM