V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
peizhao2017
V2EX  ›  问与答

请教 PHP array_intersect() 函数用法

  •  
  •   peizhao2017 · 2019-07-15 16:07:31 +08:00 · 1368 次点击
    这是一个创建于 1718 天前的主题,其中的信息可能已经有所发展或是发生改变。
    <?php

    $a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");

    $a2=array("e"=>"red","f"=>"black","g"=>"purple");

    $a3=array("a"=>"red","b"=>"black","h"=>"yellow");


    $result=array_intersect($a1,$a2,$a3);

    print_r($result);

    ?>


    官方教程里 array_intersect 里手工指定 $a1,$a2,$a3


    怎么样动态的向 array_intersect 里面传参数呢
    9 条回复    2019-07-16 20:45:42 +08:00
    Snail233
        1
    Snail233  
       2019-07-15 16:18:56 +08:00
    什么动态不动态的,你直接传不就行了。。。array_intersect([],[])
    liyaoo
        2
    liyaoo  
       2019-07-15 16:24:14 +08:00 via iPhone
    动态个数?
    peizhao2017
        3
    peizhao2017  
    OP
       2019-07-15 16:26:12 +08:00
    @liyaoo
    对对,array_intersect 里面的数组数量是变化的,要怎么传进去
    zsdroid
        4
    zsdroid  
       2019-07-15 16:31:56 +08:00
    $arr = [
    $arr1,
    $arr2,
    $arr3,
    //...
    ];
    $arr[] = $arr4;
    $arr[] = $arr5;
    call_user_func_array('array_intersect',$arr);
    ccliuxy
        5
    ccliuxy  
       2019-07-15 16:38:32 +08:00
    array_intersect(...$set)
    peizhao2017
        6
    peizhao2017  
    OP
       2019-07-15 17:03:17 +08:00
    @zsdroid 谢谢,回调函数吗


    @ccliuxy 大神没看懂
    vescape920
        7
    vescape920  
       2019-07-15 17:06:22 +08:00
    4L 和 5L 正解
    PHP5.6 以下可以使用`call_user_func_array('array_intersect',$arr);`
    PHP5.6+ 可以使用 array_intersect(...$set)
    ccliuxy
        8
    ccliuxy  
       2019-07-15 17:37:23 +08:00
    @peizhao2017
    ```
    $a = array('qqq','rrr','www');
    $b = array('www','ccc');
    $c = array('bbb', 'www', 'ccc');
    $set = array($a, $b, $c);
    print_r(array_intersect(...$set));
    ```
    文档在这里[https://www.php.net/manual/zh/functions.arguments.php#functions.variable-arg-list]( https://www.php.net/manual/zh/functions.arguments.php#functions.variable-arg-list)
    peizhao2017
        9
    peizhao2017  
    OP
       2019-07-16 20:45:42 +08:00
    感谢各位
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4042 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 10:18 · PVG 18:18 · LAX 03:18 · JFK 06:18
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.