• 007——数组(七)通过回调函数得到数组交集或差集


    <?php
    /**
     *7.通过回调函数得到数组交集或差集
     */
    
    /*array_udiff()比较2个或多个数组的差值,只比较值,不进行键名的比较
     * */
    /*$array1 = array("a" => "a", "b" => 'b', "c" => "c1");
    $array2 = array("a" => "a", "b" => 'b', "c" => "c");
    function func($v1, $v2)
    {
        if ($v1 === $v2){
            return 0;
        }elseif ($v1>$v2){
            return 1;
        }else{
            return -1;
        };
    }
    $array3 = array_udiff($array1, $array2, "func");
    print_r($array3);//输出:Array ( [c] => c1 )*/
    
    /*array_udiff_assoc() 通过回调函数,比较2个或多个数组的键名或键值的差值。
     * */
    /*$array1 = array("a1" => "a", "b" => 'b1', "c" => "c");
    $array2 = array("a" => "a", "b" => 'b', "c" => "c");
    function fun1($v1,$v2){
        if ($v1 === $v2){
            return 0;
        }elseif ($v1>$v2){
            return 1;
        }else{
            return -1;
        };
    }
    $array3=array_udiff_assoc($array1,$array2,"fun1");
    print_r($array3);//输出:Array ( [a1] => a [b] => b1 )*/
    
    /*array_udiff_uassoc() 通过回调函数,比较多个数组的键名和键值,要定义2个回调函数来处理键名和键值的比较
     * */
    /*$array1 = array("a1" => "a", "b" => 'b1', "c" => "c");
    $array2 = array("a" => "a", "b" => 'b', "c" => "c");
    function func_date($v1,$v2){
        if ($v1 === $v2){
            return 0;
        }elseif ($v1>$v2){
            return 1;
        }else{
            return -1;
        };
    }
    
    function func_key($v1,$v2){
        if ($v1 === $v2){
            return 0;
        }elseif ($v1>$v2){
            return 1;
        }else{
            return -1;
        };
    }
    $array3=array_udiff_uassoc($array1,$array2,"func_date","func_key");
    print_r($array3);//输出:Array ( [a1] => a [b] => b1 )*/
    
    /*array_uintersect() 得到多个数组中键值的交集,通过回调函数:
     * */
    /*$array1 = array("a" => "a1", "b" => 'b1', "c" => "c");
    $array2 = array("a" => "a1", "b" => 'b', "c" => "c");
    function func5($v1, $v2)
    {
        if ($v1 === $v2) {
            return 0;
        } elseif ($v1 >$v2) {
            return 1;
        } else {
            return -1;
        };
    }
    
    $array3 = array_uintersect($array1, $array2, "func5");
    print_r($array3);//输出:Array ( [a] => a1 )*/
    
    /*array_uintersect_assoc() 得到多个数组中的键值的交集,通过函数,键名也参与比较
     * */
    /*$array1 = array("a" => "a", "b" => 'b1', "c" => "c");
    $array2 = array("a" => "a", "b" => 'b', "c1" => "c");
    function func6($v1, $v2)
    {
        if ($v1 === $v2) {
            return 0;
        } elseif ($v1 > $v2) {
            return 1;
        } else {
            return -1;
        };
    }
    
    $array3 = array_uintersect_assoc($array1, $array2, "func6");
    print_r($array3);//输出:Array ( [a] => a )*/
    
    
    /*array_uintersect_uassoc()  通过2个回调函数,对多个数组的键名和键值进行比较,得到多个数组的交集元素。
     * */
    $array1 = array("a" => "a", "b" => 'b1', "c" => "c");
    $array2 = array("a" => "a", "b" => 'b', "c1" => "c");
    function func_data($v1, $v2)
    {
        if ($v1 === $v2) {
            return 0;
        } elseif ($v1 > $v2) {
            return 1;
        } else {
            return -1;
        };
    }
    function func_key($v1, $v2)
    {
        if ($v1 === $v2) {
            return 0;
        } elseif ($v1 > $v2) {
            return 1;
        } else {
            return -1;
        };
    }
    $array3 = array_uintersect_uassoc($array1, $array2, "func_data","func_key");
    print_r($array3);//输出:Array ( [a] => a )
    

      

  • 相关阅读:
    ms sql server缓存清除与内存释放
    Linux学习23-Xftp上传文件显示乱码问题
    jmeter压测学习1-window环境准备与案例
    Linux学习22-文件上传与下载(rz,sz)
    httprunner学习16-locust性能测试
    httprunner学习15-运行用例命令行参数详解
    httprunner学习14-完整的项目结构设计
    httprunner学习13-环境变量.env
    httprunner学习12-hook 机制实现setup和teardown
    httprunner学习11-辅助函数debugtalk.py
  • 原文地址:https://www.cnblogs.com/yiweiyihang/p/7824689.html
Copyright © 2020-2023  润新知