我有以下名为array
的{{1}}:
$trainer
如何Array (
[0] => stdClass Object (
[id] => 2
[firstnamephonetic] =>
[lastnamephonetic] =>
[middlename] =>
[alternatename] =>
[firstname] => Admin
[lastname] => User
[email] => sam@mailinator.com
)
[1] => stdClass Object (
[id] => 4
[firstnamephonetic] =>
[lastnamephonetic] =>
[middlename] =>
[alternatename] =>
[firstname] => Bill
[lastname] => Clinton
[email] => billclintonteaser2005@mailinator.com
)
[2] => stdClass Object (
[id] => 3
[firstnamephonetic] =>
[lastnamephonetic] =>
[middlename] =>
[alternatename] =>
[firstname] => Tom
[lastname] => Beggins
[email] => tom@mailinator.com
)
)
get only
的{{1}}和first
,所以当我想lastname
时,它会显示如下:
管理员用户,比尔克林顿,汤姆贝金斯
答案 0 :(得分:1)
你有一个对象数组。您需要遍历每个,然后使用->
:
$result = [];
foreach ($trainer as $t) {
$result[] = "$t->firstname $t->lastname";
}
echo implode(', ', $result);
答案 1 :(得分:0)
这是一个对象数组,因此您可以使用foreach
处理所有数组
但是,您必须使用->
对象表示法
$line = '';
foreach ( $trainer as $person) {
$line .= $person->firstname . ' ' . $person->lastname. ', ';
}
echo trim($line, ', ');
或
$line = '';
foreach ( $trainer as $person) {
$line .= sprintf('%s %s, ',
$person->firstname,
$person->lastname);
}
echo trim($line, ', ');
答案 2 :(得分:0)
试试这个
foreach($trainer as $val){
echo $val->firstname." ". $val->lastname;
}
答案 3 :(得分:-1)
尝试
foreach($trainer as $val){
echo $val['Name_of_your_index_'];
// write your code here
}