0

I have created two Arrays and i now want to get the matching element count based on these 2 Arrays.

Below is the Array Structures:

Array
(
    [skill] => Array
        (
            [0] => 6
            [1] => 10024
            [2] => 2
            [3] => 17
            [4] => 16
        )

)
Array
(
    [skill] => Array
        (
            [0] => 6
            [1] => 2
            [2] => 17
        )

)

Here three elements match and both array can have any numbers of skills/elements...... i need to find the matching element count based on these 2 Arrays.

I tried array_intersect() but it gave below result:

What i got:

Array
(
    [skill] => Array
        (
            [0] => 6
            [1] => 10024
            [2] => 2
            [3] => 17
            [4] => 16
        )

)

Basically i need to find the "count" of matching elements in both Arrays or return only matching elements from which i can get count.

Wolverine
  • 455
  • 3
  • 8
  • 26

3 Answers3

4

You almost had it:

$count = count(array_intersect ($arr1['skill'], $arr2['skill']));

Since you actually want to compare the arrays inside skill, you must access them.

Array_intersect gives you an array of the items that are in both, so I wrapped the whole thing with the count function which returns you the size of that array hence giving you the number of similar items.

Antony D'Andrea
  • 991
  • 1
  • 16
  • 35
0

You can also use

$a=array(
    'skill'=>array(
        0=>6,
        1=>10024,
        2=> 2,
        3=> 17,
        4 => 16, 
        )
    );

$b=array(
    'skill'=>array(
        0=>6,
        1=>2,
        2=> 17,
        )
    );
$array=array_merge_recursive($a, $b);
echo count($array['skill'])-count(array_unique($array['skill']));
Ram Choubey
  • 265
  • 2
  • 12
-2

You are on correct way. User below code for count.

$a1['skill'] = array("6", "10024", "2", "17", "16");
$a2['skill'] = array("6", "2", "17");

$result = count(array_intersect($a1['skill'], $a2['skill']));
echo $result; 
Ronak Chauhan
  • 681
  • 1
  • 8
  • 22