2012-02-12 84 views
0

我通過affiliateservice api獲取TransactionList。 我的print_r($響應)給我這個(我不得不把XX,但實際上有一些數字,有):解析affiliateservice API響應

stdClass Object 
(
    [getTransactionListReturn] => Array 
     (
      [0] => stdClass Object 
       (
        [iId] => xx 
        [sStatus] => pending 
        [sType] => normal 
        [sIp] => xx 
        [bPaid] => xx 
        [iPaymentId] => xx 
        [iMerchantId] => xx 
        [fSaleAmount] => xx 
        [fCommissionAmount] => xx 
        [dClickDate] => xx 
        [dTransactionDate] => xx 
        [sClickref] => xx 
        [aTransactionParts] => Array 
         (
          [0] => stdClass Object 
           (
            [sCommissionGroupName] => STANDARD 
            [fSaleAmount] => xx 
            [fCommissionAmount] => xx 
            [iCommission] => xx 
            [sCommissionType] => percentage 
           ) 

         ) 

       ) 

      [1] => stdClass Object 
       (
        [iId] => x 
        [sStatus] => pending 
        [sType] => normal 
        [sIp] => x 
        [bPaid] => 
        [iPaymentId] => x 
        [iMerchantId] => x 
        [fSaleAmount] => x 
        [fCommissionAmount] => x 
        [dClickDate] => x 
        [dTransactionDate] => x 
        [sClickref] => x 
        [sSearchSiteName] => x 
        [sSearchSiteKeyword] => x 
        [aTransactionParts] => Array 
         (
          [0] => stdClass Object 
           (
            [sCommissionGroupName] => DEFAULT 
            [fSaleAmount] => x 
            [fCommissionAmount] => x 
            [iCommission] => x 
            [sCommissionType] => percentage 
           ) 

         ) 

       ) 

     ) 

    [getTransactionListCountReturn] => stdClass Object 
     (
      [iRowsReturned] => 2 
      [iRowsAvailable] => 386 
     ) 

) 

所以,我的問題是我怎麼能「解析」這在PHP?我的意思是我怎樣才能訪問這些數據?我的意思是類似於

$response[0]->sStatus; 

謝謝你的幫助!

回答

1

一切都是stdClass的實例,它是PHP中的標準類(除了數組,當然是:))。

這意味着您將使用->來訪問每個對象的屬性。

$someSaleAmount = 
     $response->getTransactionListReturn[0]->aTransactionParts[0]->fSaleAmount; 

你可能從使用json_decode()得到了這個。如果您寧願處理數組而不是對象,則可以將第二個參數設置爲true

1

那麼,第一件事是對象,比它的數組,而不是對象的一些值。 我會去這樣的事情:

<code> 
$response = $obj->getTransactionListReturn(); 
foreach($response as $key => $value) { 
    // key is index (0, 1, ..., n) 
    // value is the variable like iId 
    echo $value->id; 
} 
</code> 

應該工作。