2016-04-01 100 views
0

我有以下array命名$trainer獲取數組值

Array (
    [0] => stdClass Object (
      [id] => 2 
      [firstnamephonetic] => 
      [lastnamephonetic] => 
      [middlename] => 
      [alternatename] => 
      [firstname] => Admin 
      [lastname] => User 
      [email] => [email protected] 
     ) 
    [1] => stdClass Object (
      [id] => 4 
      [firstnamephonetic] => 
      [lastnamephonetic] => 
      [middlename] => 
      [alternatename] => 
      [firstname] => Bill 
      [lastname] => Clinton 
      [email] => [email protected] 
     ) 
    [2] => stdClass Object (
      [id] => 3 
      [firstnamephonetic] => 
      [lastnamephonetic] => 
      [middlename] => 
      [alternatename] => 
      [firstname] => Tom 
      [lastname] => Beggins 
      [email] => [email protected] 
     ) 
) 

如何get onlytrainersfirstlastname,所以當我想display他們會告訴他們如下:

管理員用戶,Bill Clinton,Tom Beggins

+1

你有什麼trid? – Miki

回答

1

您有一組對象。你需要通過每個迭代,然後獲得與->

$result = []; 
foreach ($trainer as $t) { 
    $result[] = "$t->firstname $t->lastname"; 
} 
echo implode(', ', $result); 
-1

嘗試

foreach($trainer as $val){ 

    echo $val['Name_of_your_index_']; 
    // write your code here 


    } 
+1

請解釋您的答案如何解決問題,它將幫助每個人更清楚地瞭解您的解決方案併爲將來的參考 – Aziz

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, ', '); 
0

試試這個

foreach($trainer as $val){ 
echo $val->firstname." ". $val->lastname; 
} 
+1

請解釋您的答案如何解決問題,它將幫助每個人更清楚地瞭解您的解決方案,以備將來參考。 – Aziz

+0

數組是具有標準類對象的多維數組。所以我給了上面的代碼.. – Siva