-3

I have two Array say Array1 and Array2

Array1
(
    [0] => Array
        (
            [name] => abc

        )

    [1] => Array
        (
            [name] => xyz

        )
)

Array2
(
    [0] => Array
        (
            [name] => abc

        )

    [1] => Array
        (
            [name] => qwe

        )
)

I want to fetch common value from both Array so the FinalArray should be:

FinalArray
(
    [0] => Array
        (
            [name] => abc

        )

)

I tried this : in_array function But it returns nothing just a blank array.

Aashi
  • 389
  • 1
  • 9
  • 20

3 Answers3

1

@Aashi simply use array_intersect like below:

<?php
    $finalArr = array_intersect(array_column($Array1, "name"), array_column($Array2, "name"));
    print_r($finalArr);
lazyCoder
  • 2,544
  • 3
  • 22
  • 41
  • 2
    I don't encourage this! Let OP try by his own. It hardly takes 1 minute to post an answer. But you are blocking OP's learning curve – Thamilhan Apr 05 '17 at 09:50
0

Use this:

$intersect = array_uintersect($array1, $array2, 'compareDeepValue');
print_r($intersect);

function compareDeepValue($val1, $val2)
{
   return strcmp($val1['name'], $val2['name']);
}
Gaurav
  • 721
  • 5
  • 14
  • 1
    When you grab an answer, why don't you mention it? Question is already duplicate one: http://stackoverflow.com/a/5653507/5447994 – Thamilhan Apr 05 '17 at 09:54
0

Please try this:

 $array1 = array("5","26","38","42");

 $array2 = array("15","36","38","42");

 $result = array_intersect($array1, $array2);

 print_r($result);
Therichpost
  • 1,759
  • 2
  • 14
  • 19