我是一个php初学者,想要做以下事情: 当我var_dump一个名为get_values()的函数时,这就是输出:
array(2) {
[0]=> object(stdClass)#1033 (9) {
["sub_id"]=> string(1) "1"
["sub_email"]=> string(18) "mail@site.com"
["sub_name"]=> string(7) "John"
["sub_last_name"]=> string(0) ""
["sub_key"]=> string(34) "..."
["created"]=> string(19) "2015-02-17 11:37:49"
["status"]=> string(1) "1"
["sola_nl_mail_sent"]=> string(1) "0"
["sola_nl_mail_sending_time"]=> string(19) "0000-00-00 00:00:00"
}
[1]=> object(stdClass)#858 (9) {
["sub_id"]=> string(2) "10"
["sub_email"]=> string(19) "mail2@site.com"
["sub_name"]=> string(8) "Doe"
["sub_last_name"]=> string(0) ""
["sub_key"]=> string(34) "..."
["created"]=> string(19) "2015-02-24 03:04:58"
["status"]=> string(1) "1"
["sola_nl_mail_sent"]=> string(1) "1"
["sola_nl_mail_sending_time"]=> string(19) "0000-00-00 00:00:00"
}
}
现在我想输出一个只包含电子邮件的数组:
$ emails = array(' mail@site.com',' mail2@site.com');
有人可以帮助我,存档吗?
答案 0 :(得分:2)
这应该适合你:
<?php
$data = get_values();
$emails = array();
foreach($data as $v)
$emails[] = $v->sub_email;
print_r($emails);
?>
答案 1 :(得分:1)
用户foreach:
$arrData = get_values();
$emails = array();
foreach($arrData as $val) {
$emails[] = $val->sub_email;
}
答案 2 :(得分:1)
$emails = array_map(function ($value) {
return $value->sub_email;
}, get_values());