duanhuantong8278 2018-11-28 13:12
浏览 153
已采纳

如何检查数组元素是否存在于另一个数组数组中

$result = array();
    $A= Array
   (
    [0] => Array
       (
        [id] => 1946
        [name] => cook
        [pic] => cookpic.png
       )

)
$B =  Array
 (
   [0] => Array
    (
        [id] => 1944
        [name] => driver
        [pic] => driver.png

    )

  [1] => Array
    (
        [id] => 934
        [name] => developer
        [pic] => developer.jpg
    )

[2] => Array
    (
        [id] => 1946
        [name] => cook
        [pic] => cookpic.png

    )

 )

What I have tried:

   $result = !empty(array_intersect($a, $b));

I have two array, I used var_dump to show them and can see them above.

What I am trying to achieve is, I want to compare two professions and want $result array which will be unique. Now if Cook is not present in $B then cook should get pushed in $B array and this will become $result else it should not get pushed.

  • 写回答

2条回答 默认 最新

  • duanchu2607 2018-11-28 13:29
    关注

    doing $result = !empty(array_intersect($a, $b)); will just tell you if there is common elements between $a and $b

    $result = array();
    $a = array(
        array(
            "id" => 1946,
            "name" => "Cook", // upper case C
            "pic" => "cookpic.png"
        ),
    
        array(
            "id" => 1946,
            "name" => "cook", // lower case c
            "pic" => "cookpic.png"
        )
    );
    
    $b = array(
        array(
            "id" => 1944,
            "name" => "driver",
            "pic" => "driver.png"
        ),
        array(
            "id" => 934,
            "name" => "developer",
            "pic" => "developer.png"
        ),
        array(
            "id" => 1946,
            "name" => "cook", // lower case c
            "pic" => "cookpic.png"
        )
    );
    
    foreach ($a as $k => $v) {
        if (in_array($v, $b) == false) {
            array_push($result, $v);
        }
    }
    
    print_r($result);
    

    you could use array_diff but it doesn't work with multi dimentionnal arrays. So you have to do it from scratch

    EDIT:

    $result = array();
    $a = array(
        array(
            "id" => 1946,
            "name" => "Cook", // upper case C
            "pic" => "cookpic.png"
        ),
    
        array(
            "id" => 1946,
            "name" => "cook", // lower case c
            "pic" => "cookpic.png"
        )
    );
    
    $b = array(
        array(
            "id" => 1944,
            "name" => "driver",
            "pic" => "driver.png"
        ),
        array(
            "id" => 934,
            "name" => "developer",
            "pic" => "developer.png"
        ),
        array(
            "id" => 1946,
            "name" => "cook", // lower case c
            "pic" => "cookpic.png"
        )
    );
    
    $m = array_merge($a, $b);
    $result = array_map("unserialize", array_unique(array_map("serialize", $m)));
    print_r($result);
    

    merge the two arrays and then apply array_unique using array_map the delete duplicates

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答