我有两个数组,它们的格式如下:
数组1(字符串变为数组):
$super = 'aa, bb, cc, dd, ee, ff';
$array = explode(", ", $super);
数组2:
$email = array(
"bb" => "email1",
"aa" => "email2",
"dd" => "email3",
"cc" => "email4",
"ee" => "email5",
"ff" => "email6"
);
目标是针对数组2上的ids
检查数组1,如果它们匹配,则返回它们的值,即email1, email2 etc..
我的尝试:
foreach($array as $index => $arra) {
echo 'code: ' . $arra . ' array' . $email[$index] . 'list';
}
它带回了以下内容:
code: aa arraylistcode: bb arraylistcode: cc arraylistcode: dd arraylistcode: ee arraylistcode: ff arraylist
答案 0 :(得分:2)
$array
的每一部分都应该是$email
数组中使用的密钥:
foreach($array as $index => $arra) {
if(isset($email[$arra])) { // check if this key exist
echo 'code: ' . $arra . ' email: ' . $email[$arra] . '<br/>';
}
}
答案 1 :(得分:1)
一线方法
$result = array_intersect_key($email, array_flip($array));
var_dump($result);
array_flip
将数组转换为['aa'=&gt; 0,'bb'=&gt; 1,...];
array_intersect_key
才会保留电子邮件数组中的值