2

I've written this code:

$aArray = [
    [0, 0, 0], 
    [1, 0, 0], 
    [2, 0, 0], 
    [3, 0, 0], 
    [4, 0, 0], 
    [5, 0, 0], 
    [6, 0, 0], 
    [7, 0, 0], 
];

$jump = [
    [0, 0, 0], 
    [1, 0, 0], 
    [9, 7, 4], 
    [3, 0, 0], 
    [4, 0, 0], 
    [5, 0, 0], 
    [6, 0, 0], 
    [7, 0, 0], 
];
var_dump(array_intersect($aArray, $jump));

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 getting filtered out? 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?

0

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

1 Comment

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.
0

To filter the first array by the second array by comparing the full-row data, use array_uintersect() to be able to access the second level of the arrays.

Code: (Demo)

var_export(array_uintersect($aArray, $jump, fn($a, $b) => $a <=> $b));

If you want to make row-for-row comparisons pairing rows by their first-level key, then use array_uintersect_assoc().

Code: (Demo)

var_export(array_uintersect_assoc($aArray, $jump, fn($a, $b) => $a <=> $b));

Because of the all rows except [2] have the same first level key and row payload, both snippets above return the exact same result.


If you move the last row of $jump to be its first row, then array_unintersect_assoc() will return no rows, but array_uintersect() will still return all but [2]. Demo

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.