0

from URL : Count number of a group array in array in php

I ask question :

 $arr1 = [61,41,41,61,89,90]
 $arr2 = [61,41]
 $result = 2    

 //found 61 and 41 in $arr1  2 time; 
 //This mean : found 61 and 41 in $arr1[0] and $arr1[1] 
 //and found 61 and 41 in $arr1[3] and $arr1[2] again
 //So  $result = 2 

I follow the answer. That is code:

 $arr1 = array(61,41,41,61,89,90);
 $arr2 = array(61,41);

 $count = array_count_values($arr1); //count values from arr1

 $result = array();
 foreach($arr2 as $row) {
    $result[$row] = array_key_exists($row, $count) ? $count[$row] : 0;
 }

 echo min($result);

But It have a bug. If I assume

 $arr1 = array(5,6,5,6,5,7);
 $arr2 = array(5,5);
 $result = 1; 

 //This mean : found 5 and 5 in $arr[0] and $arr[2]
 // but is'not found 5 and 5 again

That true result is 1. but this result is 3. Please help me to fix this bug.

1
  • Its working fine. What you suppose to get Commented May 27, 2015 at 12:12

2 Answers 2

0

You can do it with array-intersect() and array_unique()

http://php.net/manual/en/function.array-intersect.php

http://php.net/manual/en/function.array-unique.php

$tmp = array_intersect($arr1, $arr2);
$tmp = array_unique($tmp);
$result = count($res);
Sign up to request clarification or add additional context in comments.

Comments

0

Just add condition because you have to match two times more in this case

  echo ($arr2[0] == $arr2[1]) ? floor($result[0] /2) : min($result);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.