0

I have one array with values: $array_metaValue

Array ( [0] => php [1] => ajax [2] => my [3] => profile [4] => java )

and second array contains: $search_res[$e]

php
ajax

But the problem is that the count value is always one which is wrong. It should be 2.

            print_r( $array_metaValue);
            for($e=0;$e<=count($search_res);$e++){
            echo '<br>'.$search_res[$e].'<br>';
            echo '<pre>';

            $key = array_search($search_res[$e],$array_metaValue);

            if(!$key==0)
            {
                $count=$count+1;
            }

$count right now is saving 1.

Hassan Ali
  • 593
  • 1
  • 8
  • 26

3 Answers3

6

Use

$count = count(array_intersect($array_metaValue, $search_res));

array_intersect returns an array containing the elements that are in both of the input arrays.

The problem with your code is that you need to test

if ($key !== false)
Barmar
  • 741,623
  • 53
  • 500
  • 612
1

Try this

$arrInp = array('php','ajax','my','profile','java');
$arrSearch = array('php','ajax');
$count = 0;
foreach ($arrSearch as $key => $value) {
    if(in_array(trim($value), $arrInp))
        $count++;
}
echo $count;
웃웃웃웃웃
  • 11,829
  • 15
  • 59
  • 91
0
<?php
$array_metaValue=array('php','ajax','profile','java');
$search_res=array('php','ajax');
print_r($array_metaValue);
$count=0;
for($e=0;$e<count($search_res);$e++){
            echo '<br>'.$search_res[$e].'<br>';

           $key = array_search($search_res[$e],$array_metaValue);

            echo 'Key value =>'.$key. " ";
            if($key>=0)
            {
                $count=$count+1;
                 echo 'Count value =>'.$count;
            }

      }      


?>

enter image description here

Sharma Vikram
  • 2,440
  • 6
  • 23
  • 46