2013-07-29 85 views
-1

如何檢索AB_ID中的ID和名稱?我不想在foreach中循環,因爲總會有一個結果,這似乎沒有必要。 (除非它是唯一的方法)。對象中的數組中的對象?我如何檢索數據?

這是PHP。

print_r($response); // gives data below. truncated. 
QueryResult Object 
(
    [queryLocator] => 
    [done] => 1 
    [records] => Array 
     (
      [0] => stdClass Object 
       (
        [Account_Name] => business_name 
        [Account] => account_no 
        [AB] => 12344567 
        [AB_more] => stdClass Object 
         (
          [Id] => 123445 
          [Name] => Test 3 
         ) 

目前我在做什麼:

 
foreach($response as $key => $val) { 
    $name = $val->Account_Name; #returns business_name 
    $account = $val->Account; #returns account_no 
    $ab = $val->AB; #returns 12344567 

    // nothing below seems to work 
    $ab_id = $val->AB_more->Id 
    $ab_id = $val->AB_more.Id 
    $ab_id = $val->AB_more[0]->Id 

謝謝!

+0

'$ response'是什麼? –

+0

$ name和$ account是否有效?或者您對這些有問題嗎?你可以var_dump($ val-> AB_more)? – Saturnix

+0

我更新了,對不起。上面的數據是$ response的print_r –

回答

1

沒有循環可言:

$response->records[0]->Account_Name; # business_name 
$response->records[0]->Account;  # account_no 
$response->records[0]->AB;   # 12344567 
$response->records[0]->AB_more->Id; # 123445 
$response->records[0]->AB_more->Name; # "Test 3" 

一環:

foreach($response->records[0] as $result) { 
    echo $result->Account_Name; 
    echo $result->Account; 
    echo $result->AB; 
    echo $result->AB_more->Id; 
    echo $result->AB_more->Name; 
} 
+0

我的意思是說我不想要一個內部循環(我需要外部循環)。 –

+0

用你的循環更新。 –