-1

Hi I am having an array like this...

array(
   array(
      1, 3
   ),
   array(
      4, 6, 8
   ),
   array(
      2, 3, 5, 1
   )
)

Now i want to compare first element with all the element in the second row.means I want to compare 1 with 4,6 and 8.Then with 3rd row elements like 1 with 2,3,5 and 1.Likewise i want to compare

1 is exist totally two times in the given array....So the variable count1=2....likewise 3 is exist 2 times so count2=2...8 is exist only one time so count8=1....like this...

Kindly help me to solve this.Thanks in advance.

SSS
  • 67
  • 7

2 Answers2

2

If you're just after a frequency table, you can flatten it with array_merge() and use array_count_values() to get a tally:

print_r(array_count_values(call_user_func_array('array_merge', $array)));

Output:

Array
(
    [1] => 2
    [3] => 2
    [4] => 1
    [6] => 1
    [8] => 1
    [2] => 1
    [5] => 1
)
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
0

Check This answer

Or Example

function in_array_r($needle, $haystack, $strict = false) {
    foreach ($haystack as $item) {
        if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
            return true;
        }
    }

    return false;
}

Use

$b = array(array("Mac", "NT"), array("Irix", "Linux"));
echo in_array_r("Irix", $b) ? 'found' : 'not found';
Community
  • 1
  • 1
SagarPPanchal
  • 9,839
  • 6
  • 34
  • 62