1

Here's the situations: I have 2 arrays, eg:

$a=array('a','b','c','d');
$b=array('1','b','c','e');

I want to produce 2 arrays with result:

$c=array('a','d');//only element appeared on $a
$d=array('1','e');//only element appeared on $b

Do you have a clever solution?

0

3 Answers 3

2
$c = array_diff($a, $b);
$d = array_diff($b, $a);
Sign up to request clarification or add additional context in comments.

Comments

0

Sorry, my bad. It turn out a was giving the wrong array in my test. simple array_diff solved the problem: $c = array_diff($a, $b); $d = array_diff($b, $a);

Comments

0

Try using the array_diff() function:

array_diff(array1,array2,array3...)

eg:

<?php
$a1=array(0=>"Cat",1=>"Dog",2=>"Horse");
$a2=array(3=>"Horse",4=>"Dog",5=>"Fish");
print_r(array_diff($a1,$a2));
?>

Output:

Array ( [0] => Cat )

Source: http://www.w3schools.com/PHP/func_array_diff.asp

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.