php检查关联数组中是否存在值
我有以下几点:php检查关联数组中是否存在值,php,arrays,Php,Arrays,我有以下几点: $import_emails = []; $import_emails[]=[ 'to'=>$to, 'from'=>$from, 'cc'=>$cc, 'subject'=>$subject, 'text'=>$text, 'date'=> date('Y-m-d H:i:s',strtotime($date)) ]; 阵列数据示例: Array ( [0] =>
$import_emails = [];
$import_emails[]=[
'to'=>$to,
'from'=>$from,
'cc'=>$cc,
'subject'=>$subject,
'text'=>$text,
'date'=> date('Y-m-d H:i:s',strtotime($date))
];
阵列数据示例:
Array
(
[0] => Array
(
[to] => nastya.gorobets95@gmail.com
[from] => babboe1 babboe1 <test.babboe1@gmail.com>
[cc] =>
[subject] => Test Subject
[text] => Test content.
Please, write me later
Thanks!
[date] => 2017-06-29 18:04:53
)
[1] => Array
(
[to] => Anastasia Gorobets <nastya.gorobets95@gmail.com>
[from] => babboe1 babboe1 <test.babboe1@gmail.com>
[cc] => babboesignal@edu-crm.com
[subject] => Tema
[text] => Bla bla bla
Test email! :)
[date] => 2017-07-02 11:55:50
)
)
数组
(
[0]=>阵列
(
[致]=>纳斯蒂亚。gorobets95@gmail.com
[来自]=>babboe1 babboe1
[cc]=>
[受试者]=>测试受试者
[文本]=>测试内容。
请稍后给我写信
谢谢
[日期]=>2017-06-29 18:04:53
)
[1] =>阵列
(
[至]=>阿纳斯塔西娅·戈罗贝茨
[来自]=>babboe1 babboe1
[cc]=>babboesignal@edu-crm.com
[主题]=>Tema
[文本]=>Bla Bla Bla Bla
测试电子邮件!:)
[日期]=>2017-07-02 11:55:50
)
)
如何检查数组项['to']中是否存在值,例如'nastya'?
也许它有一些功能?谢谢 您需要对阵列进行访问
foreach ($import_emails as $key => $value) {
$to = $value['to'];
if($to == "nastya") {
echo 'Found!';
break;
}
}
您要检查您的值是否存在于该数组值中。
(您想在数组的to字段中找到“nastya”)
有时,我使用将关联数组转换为平面数组。然后检查它是否存在
$dataSubjectsValue = array_column($data, 'subject');
if (in_array('Test Subject', $dataSubjectsValue)) {
// ...do your stuff
}
我认为这比使用foreach循环更具可读性。
foreach
+strpos
-自己尝试一下。如果您有问题,请用您尝试过的内容编辑您的问题。查看array\u filter()
和array\u search()
。这应该会给你一些想法或者检查一下这个:
$dataSubjectsValue = array_column($data, 'subject');
if (in_array('Test Subject', $dataSubjectsValue)) {
// ...do your stuff
}