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

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

$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
    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

    点赞 评论
  • dongzuoyue6556
    dongzuoyue6556 2018-11-28 13:41

    Use strcasecmp() Inbuilt function for comparison.And then use array_push(). You can use Brute force method. But i am sure that you can get better answer from this.

    $result=array();
    $A[0]=array("id" => 1946, "name" => Cook, "pic" => cookpic.png);
    $B[0]=array("id" => 1944, "name" => driver, "pic" => driver.png);
    $B[1]=array("id" => 934, "name" => developer, "pic" => developer.jpg);
    $B[2]=array("id" => 1946, "name" => cook, "pic" => cookpic.png);
    
    
    foreach ($A as $key => $value) 
    {
        foreach ($B as $key1 => $value1) 
        {
            if (!strcasecmp($value["name"], $value1["name"])) 
            {   array_push($result, $value);    }
        }
    }
    
    echo("<pre>");print_r($result);
    
    点赞 评论

相关推荐