-1

i have two arrays as following

$fix=array(2,4,5,6);
$marks=array(2,4,6,8);

i want to get if same number has same index in two arrays count it and i want to get final total as 2, i wrote some code but it not work i refer for this code php array comparison index by index

$total=0;
$r = array_map(function($fix, $marks) {
    if( $fix === $marks){

        $total=$total+1;
        return  $total;

        }
}, $fix, $marks);

echo $total;
1
  • count(array_intersect_assoc($fix, $marks)); will give what you need Commented Sep 25, 2014 at 5:31

3 Answers 3

1

I hope this will help you.

$fix=array(2,4,5,6);
$marks=array(2,4,6,8);

$r = array_map(function($fix, $marks) {
    if( $fix === $marks){
        return 1;
    }
    else {
        return 0;
    }
}, $fix, $marks);

echo array_sum($r);

Output 2

Sign up to request clarification or add additional context in comments.

Comments

0

You can use array_intersect_assoc (see http://php.net/manual/en/function.array-intersect-assoc.php)

$fix=array(2,4,5,6);
$marks=array(2,4,6,8);
echo count(array_intersect_assoc($fix, $marks));

Comments

0

it's working

$fix=array(2,4,5,6);
$marks=array(1,4,5,6);

    $total=0;

    foreach($fix as $key => $value)
    {
        if($value == $marks[$key])
        {
            $total=$total+1;
        }
    }

    echo $total;

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.