1

I am trying to compare two non-associative arrays to create a new array with the matches.

This is what I have though:

//This array has several entries
$a_firstarray = Array();
//This array has less entries than the first array
$a_secondarray = Array();
//This array should contain the matches of the first array and the second array in no particular order
$a_mergedarray

    for($i=0;$i <=count($a_firstarray);$i++){
            for($a=0;$a <=count ($a_secondarray);$a++){
                if($a_firstarray[$i] == $a_secondarray[$a]){
                    $a_mergedarray[] = $a_activecategory[$i];
                }
            }
        }

It doesn't work for some reason. I am also sure that PHP has some sort of function that does this. Any ideas? thanks in advance.

1

3 Answers 3

1

This is known as the "intersection" of two arrays. PHP provides array_intersect.

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

Comments

1

use array_intersect.

$result = array_intersect($array1, $array2);

Comments

1

Are you looking for array_intersect()? http://php.net/manual/en/function.array-intersect.php

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.