如何匹配两个数组的值?

时间:2012-10-02 18:48:46

标签: php arrays

使用PHP,如何将订阅者与具有这两个阵列的用户匹配?

$订户

Array
(
    [0] => stdClass Object
        (
            [meta_id] => 1
            [email] => name@example.com
        )

)

$用户

Array
(
    [0] => stdClass Object
        (
            [ID] => 1
            [user_email] => name@example.com
        )

    [1] => stdClass Object
        (
            [ID] => 2
            [user_email] => name2@example.com
        )

我以为会这样做......

<?php
foreach ( $users as $user ) {
    if ( in_array( 'name@example.com', $subscribers ) ) {
        echo 'Matched user';
    }
}

1 个答案:

答案 0 :(得分:3)

foreach ($users as $user) {
    foreach ($subscribers as $subscriber) {
        if ($user->user_email === $subscriber->email) {
            echo 'Matched user';
            break;
        }
    }
}