乐趣区

关于php:PHP-求取数组的差集

求取关联数组的差集

交并差示例图

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

 有时在业务逻辑当中咱们要获取两个数组的差集, 本意是想获取差集, 即图中 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 
          => 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 
         => 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…

\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 (
         => 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
    )
    */
退出移动版