2

I've written this code:

<?php

$aArray = array
( 
array(0,0,0), 
array(1,0,0),
array(2,0,0),
array(3,0,0),
array(4,0,0),
array(5,0,0),
array(6,0,0),
array(7,0,0)
);

$jump = array
( 
array(0,0,0), 
array(1,0,0),
array(9,7,4),
array(3,0,0),
array(4,0,0),
array(5,0,0),
array(6,0,0),
array(7,0,0)
);

$result = array_intersect($aArray, $jump);

echo var_dump($result);

the result I'm getting is this:

array(8) { 
[0]=> array(3) { 
    [0]=> int(0) 
    [1]=> int(0) 
    [2]=> int(0) } 
[1]=> array(3) { 
    [0]=> int(1) 
    [1]=> int(0) 
    [2]=> int(0) } 
[2]=> array(3) { 
    [0]=> int(2) 
    [1]=> int(0) 
    [2]=> int(0) } 
[3]=> array(3) { 
    [0]=> int(3) 
    [1]=> int(0) 
    [2]=> int(0) } 
[4]=> array(3) { 
    [0]=> int(4) 
    [1]=> int(0) 
    [2]=> int(0) } 
[5]=> array(3) { 
    [0]=> int(5) 
    [1]=> int(0) 
    [2]=> int(0) } 
[6]=> array(3) { 
    [0]=> int(6) 
    [1]=> int(0) 
    [2]=> int(0) } 
[7]=> array(3) { 
    [0]=> int(7) 
    [1]=> int(0) 
    [2]=> int(0) } 
    }

why isn't the second index returning null? I've tried emptying my cache in case it had old values stored in there. I've also noticed that if I delete the last array from the jump array, it still produces 7,0,0. Is this a weird anomaly?

Apul Gupta
  • 3,044
  • 3
  • 22
  • 30
upndown
  • 25
  • 5

1 Answers1

2

array_intersect() is not recursive, it sees the inner arrays as just an array. You would need to use something like this:

function array_intersect_recursive() {

    foreach(func_get_args() as $arg) {
        $args[] = array_map('serialize', $arg);
    }
    $result = call_user_func_array('array_intersect', $args);

    return array_map('unserialize', $result);
}

$result = array_intersect_recursive($aArray, $jump);
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
  • 1
    thanks @AbraCadaver. I suspected it had something to do with requiring a recursive function. very helpful. I also figured out that when it's returning the intersect point, if a nested array has at least one key/ value match, it will return the entire thing because it's returning the first parameter's keys. – upndown May 09 '15 at 20:50