4

I've read some tutorials on here but none of them return what I need. I have two arrays.

$arrayA = array(1960,1961,1963,1962,1954,1953,1957,1956,1955);
$arrayB = array(1949,1960,1961,1963,1962,1954,1953,1957,1956,1955);

However, when I run array_diff, it returns an empty array.

$diff = array_diff($arrayA, $arrayB);

But I'd like it to return 1949. What's the error in my code?

edit: since switching the variables won't work, i did var_dump for the 3 arrays (A, B, and diff) and here's the pastebin http://pastebin.com/tn1dvCs3

1
  • 3
    1949 is in both of the arrays in pastebin. What is the expected output? Commented Apr 18, 2013 at 4:17

6 Answers 6

9

array_diff works by finding the elements in the first array that aren't in the second, per the documentation. Try inverting your call:

$diff = array_diff($arrayB, $arrayA);

To see this in action, lets look at a more manageable but equivalent example:

$arrayA = array(1960);
$arrayB = array(1949,1960);

$diff = array_diff($arrayB, $arrayA);
var_dump($diff);

This yields:

[mylogin@aserver ~]$ vim test.php
[mylogin@aserver ~]$ php test.php
array(1) {
  [0]=>
  int(1949)
}

Please note that this uses a minimal demonstrative example of the functionality you're attempting to get. By discarding unnecessary data in your actual implementation you can more quickly zero in on the problem you're having.

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

2 Comments

still returning an empty array for me. i updated the main post with more info
@user2036066 I have tested this code, as shown above, and it works. Your pastebin shows two arrays with no diff: they contain the same elements, so naturally this won't yield anything.
2

Returns an array containing all the entries from array1 that are not present in any of the other arrays.

Therefore:

$diff = array_diff($arrayB, $arrayA);

will give you the result you're after.

1 Comment

Still returning an empty array for me
1

Swap variables in array_diff call:

<?php
$arrayA = array(1960,1961,1963,1962,1954,1953,1957,1956,1955);
$arrayB = array(1949,1960,1961,1963,1962,1954,1953,1957,1956,1955);

$diff = array_diff($arrayB, $arrayA);

var_dump($diff);
?>

Shows:

array(1) { [0]=> int(1949) }

Comments

1

Give,

$diff = array_diff($arrayB, $arrayA);

To have result of 1949, ie the element $arrayB has and $arrayA has not.

ie

$arrayA = array(1960,1961,1963,1962,1954,1953,1957,1956,1955);
$arrayB = array(1949,1960,1961,1963,1962,1954,1953,1957,1956,1955);

$diff = array_diff($arrayB, $arrayA);

print_r($diff );

Test Here

Comments

0

you can read document here http://php.net/manual/pt_BR/function.array-diff.php

$diff = array_diff($arrayB, $arrayA);
print_r($diff);

Comments

0
$a = '5,6,'; 
$b = '6,6,'; 
$a_array = explode(',',$a); 
$b_array = explode(',',$b);
$result = array_diff($b_array,$a_array); 
var_dump($result);

However, when I run array_diff, it returns an empty array.

$result = array_diff($b_array,$a_array); 

But I'd like it to return 6. What's the error in my code?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.