0

I have two arrays one that has just one index which list id's. The other array has many indexes but I want to compare only the id index of both arrays which is index [0] I want to get only the rows that ids don't match and return those rows. Example:

$array1 = [
    '12345',
    '23457'
];

$array2 = [
   [id => '12345', 'fake_data' => 'something'],
   [id => '23457', 'more_data' => 'something else'],
   [id => '76389','more_data' => 'something else 3'],
   [id => '10293', 'more_data' => 'something else 4'],
   [id => '09229', 'more_data' => 'something else 5']
];

The first array just have id's the other has ids place other data. I need to keep that data and only return the rows that don't match the first array. Help is very much appriciated.

3 Answers 3

2
$array1 = [
    '12345',
    '23457'
];

$array2 = [
   ['id' => '12345', 'fake_data' => 'something'],
   ['id' => '23457', 'more_data' => 'something else'],
   ['id' => '76389','more_data' => 'something else 3'],
   ['id' => '10293', 'more_data' => 'something else 4'],
   ['id' => '09229', 'more_data' => 'something else 5']
];

$found_id_rows = array_filter($array2, function($row) use($array1) {
    return in_array($row['id'], $array1);
});

$not_found_id_rows = array_filter($array2, function($row) use($array1) {
    return !in_array($row['id'], $array1);
});

Gives:

Array
(
    [0] => Array
        (
            [id] => 12345
            [fake_data] => something
        )

    [1] => Array
        (
            [id] => 23457
            [more_data] => something else
        )

)
Array
(
    [2] => Array
        (
            [id] => 76389
            [more_data] => something else 3
        )

    [3] => Array
        (
            [id] => 10293
            [more_data] => something else 4
        )

    [4] => Array
        (
            [id] => 09229
            [more_data] => something else 5
        )

)

https://3v4l.org/6OVjP

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

1 Comment

I wish I could up vote you both, but I can't so I'll have to up vote the first answer. I didn't use either both all three ways work so. Thanks for the input.
1
<?php

$array1 = [
    '12345',
    '23457'
];

$array2 = [
   ['id' => '12345', 'fake_data' => 'something'],
   ['id' => '23457', 'more_data' => 'something else'],
   ['id' => '76389','more_data' => 'something else 3'],
   ['id' => '10293', 'more_data' => 'something else 4'],
   ['id' => '09229', 'more_data' => 'something else 5']
];

foreach($array2 as $key => $array)
    if(array_search($array['id'], $array1) !== false)
        unset($array2[$key]);

var_export($array2);

Output:

array (
    2 => 
    array (
      'id' => '76389',
      'more_data' => 'something else 3',
    ),
    3 => 
    array (
      'id' => '10293',
      'more_data' => 'something else 4',
    ),
    4 => 
    array (
      'id' => '09229',
      'more_data' => 'something else 5',
    ),
  )

1 Comment

As in Jarad's answer you could swap the conditional for: if(in_array($array['id'], $array1)). If you don't want to mutate the second array, copy it first.
0

I think it looks like an array_intersect with two different types of arrays. To handle the different array types, you can use array_uintersect, which takes a callback to define the comparison between items.

$result = array_uintersect($array2, $array1, function($a, $b) {
    return ($a['id'] ?? $a) <=> ($b['id'] ?? $b);
});

The two terms ($a['id'] ?? $a) and ($b['id'] ?? $b) in the callback are like that because they can be terms from either $array1 or $array2.

I don't know if this is any better or worse than the other solutions; it's just a different way of looking at it.

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.