求取关联数组的差集

交并差示例图

diff.png

array_diff 函数官方文档截图:https://www.php.net/manual/zh...

微信截图_20201224102750.png

有时在业务逻辑当中我们要获取两个数组的差集,本意是想获取差集,即图中 A-B=a+b 部分,不包含 c 部分.

PHP 内置函数中首先想到的是 array_diff() 函数,但 array_diff() 函数传参中第一个 array1 是对比的参照数据,返回的数据是在 array1 中不在其他 array 中的值,显然这并不符合我们的预期.我们可以根据文档测试下 array_diff() 函数返回差集.
示例代码
<?php
    //关联数组实例
    $array1 = ["a" => "green", "b" => "red", "c" => "blue", "d" => "pink"];
    $array2 = ["a" => "green", "b" => "white", "e" => "yellow", "f" => "pink"];
    $result = array_diff($array1, $array2);
    print_r($result);
    /*
    输出内容
    Array
    (
         [b] => red 
         [c] => blue
    )
    */

    //索引数组实例
    $array1 = ["green", "red", "blue", "pink"];
    $array2 = ["green", "white", "yellow", "pink", "black"];
    $result = array_diff($array1, $array2);
    print_r($result);
    /*
    输出内容
    Array ( 
        [1] => red 
        [2] => blue
    )
    */

    //文档已经说函数的返回内容,但是可以多次使用 array_diff() 函数对比两个数组来实现我们的目的
    //关联数组
    $array1 = ["a" => "green", "b" => "red", "c" => "blue", "d" => "pink"];
    $array2 = ["a" => "green", "b" => "white", "e" => "yellow", "f" => "pink"];
    $result1 = array_diff($array1, $array2);
    $result2 = array_diff($array2, $array1);
    $result3 = array_merge($result1,$result2);
    print_r($result3);
    /*
    输出内容
    Array (
        [b] => white 
        [c] => blue 
        [e] => yellow
    )
    */

    //索引数组
    $array1 = ["green", "red", "blue", "pink"];
    $array2 = ["green", "white", "yellow", "pink", "black"];
    $result1 = array_diff($array1, $array2);
    $result2 = array_diff($array2, $array1);
    $result3 = array_merge($result1,$result2);
    print_r($result3);
    /*
    输出内容
    Array (
        [0] => red 
        [1] => blue 
        [2] => white 
        [3] => yellow
        [4] => black
    )
    */
其他方式
从 PHP 官方文档中我们发现有一个 DS 扩展类,位置:"PHP 手册->函数参考->其它基本扩展->Data Structures",从下图中可以看到简介"PHP7高效的数据结构,可以作为 array 的替代",这是 PHP7 中对 array 的一个替代品,通过仔细阅读可知 DS 包含的数据结构能够很好的替代 array 数据结构,提供了更多的数据结构选择.
Data Structures :https://www.php.net/manual/zh...

微信截图_20201224110302.png

\Ds\Set 和 \Ds\Map 扩展使用实例
<?php
    //使用 DS 扩展需要手动安装,可以使用 phpize(https://pecl.php.net/package/ds)方式也可以pecl install ds 方式
       //关联数组
    //Ds\Set()
    $array1 = ["a" => "green", "b" => "red", "c" => "blue", "d" => "pink"];
    $array2 = ["a" => "green", "b" => "white", "e" => "yellow", "f" => "pink"];
    $tpm_1 = new \Ds\Set($array1);
    $tpm_2 = new \Ds\Set($array2);
    print_r($tpm_1->xor($tpm_2)->toArray());
    /*
    输出内容
    Array (
        [0] => red 
        [1] => blue 
        [2] => white 
        [3] => yellow
    )
    */
    //Ds\Map()
    $array1 = ["a" => "green", "b" => "red", "c" => "blue", "d" => "pink"];
    $array2 = ["a" => "green", "b" => "white", "e" => "yellow", "f" => "pink"];
    $tpm_1 = new \Ds\Map($array1);
    $tpm_2 = new \Ds\Map($array2);
    print_r($tpm_1->xor($tpm_2)->toArray());
    /*
    输出内容
    Array (
        [c] => blue 
        [d] => pink 
        [e] => yellow 
        [f] => pink
    )
    */
    //索引数组
    //Ds\Set()
    $array1 = ["green", "red", "blue", "pink"];
    $array2 = ["green", "white", "yellow", "pink", "black"];
    $tpm_1 = new \Ds\Set($array1);
    $tpm_2 = new \Ds\Set($array2);
    print_r($tpm_1->xor($tpm_2)->toArray());
    /*
    输出内容
    Array (
        [0] => red 
        [1] => blue 
        [2] => white 
        [3] => yellow 
        [4] => black
    )
    */
    //Ds\Map()
    $array1 = ["green", "red", "blue", "pink"];
    $array2 = ["green", "white", "yellow", "pink", "black"];
    $tpm_1 = new \Ds\Map($array1);
    $tpm_2 = new \Ds\Map($array2);
    print_r($tpm_1->xor($tpm_2)->toArray());
    /*
    输出内容
    Array (
        [4] => black
    )
    */

其名
10 声望2 粉丝

Hello World!