如果值匹配,则从特定数组获取值[重复]

问题描述 投票:0回答:1

如果电子邮件匹配,则从数组键 ['email'] 获取值。

如果用户的电子邮件与数组键值匹配,我想在 Wordpress 评论中打印“特殊用户”。


$match_email = '[email protected], [email protected]';


$config = [
    'special_user' => [
        'muskan' => [
            'age' => '24',
            'email' => '[email protected]'
        ],
        'khushi' => [
            'age' => '21',
            'email' => '[email protected]'
        ]
    ]
];



foreach ($config as $name => $users) {
    foreach ($users as $author => $email) {
        if (in_array($email['email'], $match_email)) {
            echo 'Special User';
            break;
        }
    }
}

如果电子邮件与数组值匹配,则打印“特殊用户”。

php arrays wordpress multidimensional-array
1个回答
-1
投票

它不起作用,因为您没有将数组传递给它:

in_array($user_data['email'], $match_email)

您需要将

$match_email
转换为带有
explode()
的数组。

<?php

$match_email = explode(', ', '[email protected], [email protected]');


$config = [
    'special_user' => [
        'muskan' => [
            'age' => '24',
            'email' => '[email protected]'
        ],
        'khushi' => [
            'age' => '21',
            'email' => '[email protected]'
        ]
    ]
];



foreach ($config as $name => $users) {
    foreach ($users as $author => $user_data) {
        if (in_array($user_data['email'], $match_email)) {
            echo 'Special User';
            break;
        }
    }
}

如果可能的话,您也可以首先使用数组:

<?php


$match_email = ['[email protected]', '[email protected]'];


$config = [
    'special_user' => [
        'muskan' => [
            'age' => '24',
            'email' => '[email protected]'
        ],
        'khushi' => [
            'age' => '21',
            'email' => '[email protected]'
        ]
    ]
];



foreach ($config as $name => $users) {
    foreach ($users as $author => $email) {
        if (in_array($email['email'], $match_email)) {
            echo 'Special User';
            break;
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.