获取数组值

时间:2016-04-01 13:49:38

标签: php

我有以下名为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时,它会显示如下:

  

管理员用户,比尔克林顿,汤姆贝金斯

4 个答案:

答案 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


    }