php 两数组之差

hsgswve4  于 2023-01-19  发布在  PHP
关注(0)|答案(7)|浏览(83)

我有以下两个数组,我想知道这两个数组的区别,也就是说,我怎样才能找到两个数组中不存在的值?

$array1=Array ( [0] => 64 [1] => 98 [2] => 112 [3] => 92 [4] => 92 [5] => 92 ) ;
 $array2=Array ( [0] => 3 [1] => 26 [2] => 38 [3] => 40 [4] => 44 [5] => 46 [6] => 48 [7] => 52 [8] => 64 [9] => 68 [10] => 70 [11] => 72 [12] => 102 [13] => 104 [14] => 106 [15] => 92 [16] => 94 [17] => 96 [18] => 98 [19] => 100 [20] => 108 [21] => 110 [22] => 112);
tvokkenx

tvokkenx1#

要获得两个数组之间的差异,您需要执行以下操作:

$fullDiff = array_merge(array_diff($array1, $array2), array_diff($array2, $array1));

原因是array_diff()只会给予你$array1中的值,而不是$array2中的值,而不是相反。

qjp7pelc

qjp7pelc2#

**注意:**此答案将返回$array2中不存在的值,不会返回$array1中不存在的值。

$diff = array_diff($array2, $array1);

array_diff()

wdebmtf2

wdebmtf23#

如果你想递归地得到数组之间的差异,试试这个函数:

function arrayDiffRecursive(array $firstArray,array  $secondArray, bool $reverse = false): array 
{
    $first = 'first';
    $second = 'second';
    if ($reverse) {
        $first = 'second';
        $second = 'first';
    }
    $diff = [];
    foreach ($firstArray as $k => $value) {
        if (!is_array($value)) {
            if (!array_key_exists($k, $secondArray) || $secondArray[$k] != $value) {
                $diff[$first][$k] = $value;
                $diff[$second][$k] = $secondArray[$k] ?? null;
            }
            continue;
        }
        if (!array_key_exists($k, $secondArray) || !is_array($secondArray[$k])) {
            $diff[$first][$k] = $value;
            $diff[$second][$k] = $secondArray[$k] ?? null;
            continue;
        }
        $newDiff = arrayDiffRecursive($value, $secondArray[$k], $reverse);
        if (!empty($newDiff)) {
            $diff[$first][$k] = $newDiff[$first];
            $diff[$second][$k] = $newDiff[$second];
        }
    }
    return $diff;
}

试验:

$firstArray = ['a' => ['b' => 'c'], 'g' => ['h' => 's']];
$secondArray = ['b' => 'c', 'd' => ['e' => 'f'], 'g' => ['i' => 'j']];
$diffs = array_replace_recursive(
    arrayDiffRecursive($firstArray, $secondArray),
    arrayDiffRecursive($secondArray, $firstArray, true)
);
print_r($diffs);

输出:

Array
(
    [first] => Array
        (
            [a] => Array
                (
                    [b] => c
                )

            [g] => Array
                (
                    [h] => s
                    [i] =>
                )

            [b] =>
            [d] =>
        )

    [second] => Array
        (
            [a] =>
            [g] => Array
                (
                    [h] =>
                    [i] => j
                )

            [b] => c
            [d] => Array
                (
                    [e] => f
                )

        )

)
sbtkgmzw

sbtkgmzw4#

<?php
function getArrayDiff($a1, $a2) {
    $result = array();

    print_r($a1);
    print_r($a2);

    // If First Array is Bigger than Second
    if( count($a1) > count($a2) ) {
        $result=array_diff($a1,$a2);
    }
    // If Second Array is Bigger than First
    if( count($a1) < count($a2) ) {
        $result=array_diff($a2,$a1);
    }
    // If Both array are same but, data values are different.
    else
    {
        $result = array_merge (array_diff($a2,$a1), array_diff($a1,$a2));   
    }
    return $result;
}

print "<pre>";
// First Array is Big
echo "First Array is Big <br/>";
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("e"=>"red","f"=>"green","g"=>"blue");
print_r( getArrayDiff($a1, $a2) );

// Second Array is Big
echo "Second Array is Big <br/>";
$a1=array("e"=>"red","f"=>"green","g"=>"blue");
$a2=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
print_r( getArrayDiff($a1, $a2) );

// Both Array are same
echo "Both Array are same <br/>";
$a1=array("a"=>"red","b"=>"green","d"=>"yellow");
$a2=array("e"=>"red","f"=>"green","g"=>"blue");
print_r( getArrayDiff($a1, $a2) );

?>

输出:

First Array is Big 
Array
(
    [a] => red
    [b] => green
    [c] => blue
    [d] => yellow
)
Array
(
    [e] => red
    [f] => green
    [g] => blue
)
Array
(
    [d] => yellow
)
Second Array is Big 
Array
(
    [e] => red
    [f] => green
    [g] => blue
)
Array
(
    [a] => red
    [b] => green
    [c] => blue
    [d] => yellow
)
Array
(
    [d] => yellow
)
Both Array are same 
Array
(
    [a] => red
    [b] => green
    [d] => yellow
)
Array
(
    [e] => red
    [f] => green
    [g] => blue
)
Array
(
    [g] => blue
    [d] => yellow
)
3yhwsihp

3yhwsihp5#

数组差异?
http://php.net/array_diff

var_dump(array_diff($array2, $array1));
jv2fixgn

jv2fixgn6#

如果您打算使用array_diff()
需要记住的是,论点的写作顺序。
PHP文件夹:array_diff

# array_diff(1, 2)
array_diff($one, $two);

# array_diff(2, 1)
array_diff($two, $one); // produces different result.
mum43rcc

mum43rcc7#

$numbers = range(000000, 999999);
$numbers = array_map(function ($n) {
    if (strlen($n) < 6) {
        $addZero = 6 - strlen($n);
        $appendZero = str_repeat('0', $addZero);
        $n = $appendZero . $n;
    }
    return $n;
}, $numbers);
$allRanges = array_diff($numbers, $getAllPurchasedTicketsArr);

相关问题