如果电子邮件匹配,则从数组键 ['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;
}
}
}
如果电子邮件与数组值匹配,则打印“特殊用户”。
它不起作用,因为您没有将数组传递给它:
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;
}
}
}