-2

this is the print_r of the arrays and the last one is the result:

Array ( [0] => 28 [1] => 29 [2] => 30 [3] => 31 [4] => 32 [5] => 33 [6] => 34 ) 

Array ( [0] => 28 [1] => 29 [2] => 30 [3] => 31 [4] => 32 [5] => 33 [6] => 34 [7] => 35 ) 

result:
Array ( [1] => 29 [2] => 30 [3] => 31 [4] => 32 [5] => 33 [6] => 34 )

I want to get an array that holds 35 entries since its the only one missing and for some reason I get entire first array if I put the second array first in the function I get all entries of the second one.

Schwesi
  • 4,753
  • 8
  • 37
  • 62
webli
  • 1
  • 6

2 Answers2

0

When using array_diff the first array is used and checked against any other arrays passed.

Your 35 is found like so:

$array1 = Array ( 28, 29, 30, 31, 32, 33, 34 );

$array2 = Array ( 28, 29, 30, 31, 32, 33, 34, 35 );

$diff = array_diff($array2, $array1);

print_r($diff);

Gives:

Array
(
    [7] => 35
)
Stuart
  • 6,630
  • 2
  • 24
  • 40
0

Using the array function array_diff() you can achieve what you are looking for. Compares $arr2 against $arr1 and returns the values in $arr2 that are not present in $arr1.

$arr1 = range(28, 34);
$arr2 = range(28, 35);

$arr = array_diff($arr2, $arr1);
print_r($arr); // Array ( [7] => 35 )
Murad Hasan
  • 9,565
  • 2
  • 21
  • 42