0

Using PHP, how can I match subscribers to users with these two arrays?

$subscribers

Array
(
    [0] => stdClass Object
        (
            [meta_id] => 1
            [email] => [email protected]
        )

)

$users

Array
(
    [0] => stdClass Object
        (
            [ID] => 1
            [user_email] => [email protected]
        )

    [1] => stdClass Object
        (
            [ID] => 2
            [user_email] => [email protected]
        )

I thought this would do it...

<?php
foreach ( $users as $user ) {
    if ( in_array( '[email protected]', $subscribers ) ) {
        echo 'Matched user';
    }
}

1 Answer 1

3
foreach ($users as $user) {
    foreach ($subscribers as $subscriber) {
        if ($user->user_email === $subscriber->email) {
            echo 'Matched user';
            break;
        }
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

I just realized why mine isn't working. $subscribers is an array. $users is an object. Have a tweak to make that work?
From what you pasted, $users is an array of objects, as is $subscribers. If it's not, then you need to fix your question to reflect the real value stored in that variable.
Nevermind. Turns out that it was just a matter of case differences! strltolower( $user->user_email ) fixed it. Thanks.
Do not use strtolower() on email addresses. The mailbox name is case-sensitive according to the RFC. In other words, in "[email protected]", "test" is case-sensitive. ("example.com" is not.) "[email protected]" and "[email protected]" are different mailboxes.

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.