7

Guys i have a very awkward situation here , i am not sure whether am i taking a right approach or not.. i am trying to match the values between 2 arrays and then running if else statement... here's goes what i am trying to do

$array1 = array('html','php','js','css');
$array2 = array('php','python','html','java');

what i want is to make a check where the values of these 2 arrays matches to each other. like php and html is common in both and also where it doesn't match.

Thanks

2
  • Take a look at PHP's built-in array functions. And maybe array-diff php.net/manual/en/function.array-diff.php Commented Nov 7, 2011 at 16:01
  • By "match", do you merely mean that the variable is an element of both arrays, or do you also require that they occupy the same position in each array? Commented Nov 7, 2011 at 16:02

4 Answers 4

7

You mean like an intersection?

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

Comments

7

It's your need:

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

the result is:

Array
(
    [0] => html
    [1] => php
)

Comments

5

array_intersect

and

array_diff

should do what you want.

Comments

3

To get both the intersecting elements of the array and the differing elements use both array_diff() and array_intersect().

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.