1

I have 2 arrays one contains just a list of email address and the other contains emails and names.

The list of emails in array1 are stored in a database and the other array is a list of new and current users.

I have tried using array_diff however this only seems to work if I specify in my loop that I just want emails.

    $array1 = array(
        0 => 'dave@daveshouse.com',
        1 => 'sam@samshouse.com',
        2 => 'jim@jimshouse.com',
        3 => 'olly@ollyshouse.com',
        4 => 'tom@tomshouse.com'
    );

    $array2 = array(
        0 => array(
            0 => 'tom',
            1 => 'tom@tomshouse.com'
        ),
        1 => array(
            0 => 'james',
            1 => 'james@jameshouse.com'
        ),
        2 => array(
            0 => 'marvin',
            1 => 'marvin@marvinshouse.com'
        ),
        3 => array(
            0 => 'jane',
            1 => 'jane@janeshouse.com'
        ),
    );

    $array2emails = array();
    foreach ($array2 as $item) {
        $array2emails[] = $item[1];
    }


    $result = array_diff($array2emails, $array1);

$results gives me

Array
(
    [1] => james@jameshouse.com
    [2] => marvin@marvinshouse.com
    [3] => jane@janeshouse.com
)

however I need to get this:

Array 
    (
        [1] => array(
            [0] => james
            [1] => james@jameshouse.com
        )
        [2] => array(
            [0] => marvin
            [1] => marvin@marvinshouse.com
        )
        [3] => array(
            [0] => jane
            [1] => jane@janeshouse.com
        )
    )

Any help would be much appreciated. Thanks

Fazberry
  • 117
  • 8

4 Answers4

1

Since the array returned by array_diff has the same keys as the original array, you can use that to get the elements of the original array back, with array_intersect_key.

$array1 = array(
    0 => 'dave@daveshouse.com',
    1 => 'sam@samshouse.com',
    2 => 'jim@jimshouse.com',
    3 => 'olly@ollyshouse.com',
    4 => 'tom@tomshouse.com'
    );

$array2 = array(
    0 => array(
        0 => 'tom',
        1 => 'tom@tomshouse.com'
        ),
    1 => array(
        0 => 'james',
        1 => 'james@jameshouse.com'
        ),
    2 => array(
        0 => 'marvin',
        1 => 'marvin@marvinshouse.com'
        ),
    3 => array(
        0 => 'jane',
        1 => 'jane@janeshouse.com'
        ),
    );

$array2emails = array_column($array2, 1);
$keys = array_diff($array2emails, $array1);
$result = array_intersect_key($array2, $keys);
print_r($result);

Result:

Array
(
    [1] => Array
        (
            [0] => james
            [1] => james@jameshouse.com
        )

    [2] => Array
        (
            [0] => marvin
            [1] => marvin@marvinshouse.com
        )

    [3] => Array
        (
            [0] => jane
            [1] => jane@janeshouse.com
        )

)
Barmar
  • 741,623
  • 53
  • 500
  • 612
0
$array1 = array(
    0 => 'dave@daveshouse.com',
    1 => 'sam@samshouse.com',
    2 => 'jim@jimshouse.com',
    3 => 'olly@ollyshouse.com',
    4 => 'tom@tomshouse.com'
);

$array2 = array(
    0 => array(
        0 => 'tom',
        1 => 'tom@tomshouse.com'
    ),
    1 => array(
        0 => 'james',
        1 => 'james@jameshouse.com'
    ),
    2 => array(
        0 => 'marvin',
        1 => 'marvin@marvinshouse.com'
    ),
    3 => array(
        0 => 'jane',
        1 => 'jane@janeshouse.com'
    ),
);

$diffs = array();
foreach ($array2 as $item) {
    if (!in_array($item[1], $array1)) {
        $diffs[] = $item;
    }
}
Eimsas
  • 492
  • 6
  • 21
0

Use array_filter function.

function filterEmail($value, $key){
    global $array1;
    return !in_array($value[1], $array1);
}

$emailDiff = array_filter($array2, 'filterEmail' , ARRAY_FILTER_USE_BOTH );

print_r($emailDiff );
Dark Knight
  • 6,116
  • 1
  • 15
  • 37
0

Since you are using a foreach loop why not create the array there? As long as the arrays are not to long this will word nicely. I even took the original key into the new array. So you will be able to do other comparison methods to it.

$array1 = [
    0 => 'dave@daveshouse.com',
    1 => 'sam@samshouse.com',
    2 => 'jim@jimshouse.com',
    3 => 'olly@ollyshouse.com',
    4 => 'tom@tomshouse.com'
];

$array2 = [
    0 => [
        0 => 'tom',
        1 => 'tom@tomshouse.com'
    ],
    1 => [
        0 => 'james',
        1 => 'james@jameshouse.com'
    ],
    2 => [
        0 => 'marvin',
        1 => 'marvin@marvinshouse.com'
    ],
    3 => [
        0 => 'jane',
        1 => 'jane@janeshouse.com'
    ],
];

$arrayExits = [];
foreach ($array2 as $key => $item) {
    if(in_array($item[1], $array1)) {
        $arrayExits[$key] = $item;
    }
}

print_r($arrayExits);
Henry
  • 1,242
  • 1
  • 12
  • 10