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

Mohammed
  • 648
  • 2
  • 12
  • 32

2 Answers2

1

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

RomMer
  • 113
  • 12
  • Thanks. Pardon me, there is some changes in que. only diff is your `$result` giving me only that array which is non-unique, But what I want in `$result` is array should be containing unique array from both `$a` and `$b`. Like if I search for "cook" so my result array should contain all element of $b with only one time $a's cook – Mohammed Nov 28 '18 at 13:45
0

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);
lifeisbeautiful
  • 817
  • 1
  • 8
  • 18