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条)

报告相同问题?

悬赏问题

  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法
  • ¥15 很想要一个很好的答案或提示
  • ¥15 扫描项目中发现AndroidOS.Agent、Android/SmsThief.LI!tr
  • ¥15 怀疑手机被监控,请问怎么解决和防止