2017-07-01 84 views
4

我有一個數組,我能夠從我的成績得到一些細節是這樣的:這樣做會顯示如下當PHP如何從一個數組中提取值

$result["transaction"]; 

{ "id": "wt13LbKmJ2", 
    "location_id": "EYGMFA", 
    "created_at": "2017-07-01T07:32:57Z", 
    "tenders": 
    [ { "id": "C6xMF", 
    "location_id": "MFA", 
    "transaction_id": "NvDAOOA9", 
    "created_at": "2017-07-01T07:32:57Z", 
    "note": "Offering", 
    "amount_money": { "amount": 100, "currency": "USD" },          "processing_fee_money": { "amount": 33, "currency": "USD" }, 
    "customer_id": "14QEJXXM5TX", 
    "type": "CARD", 
    "card_details": { "status": "CAPTURED", 
    "card": { "brand": "VI", "last_4": "0000" }, 
    "entry_method": "ON_FILE" 
    } } ], 
    "product": "EXTERNAL_API" } 

我怎樣才能讓我的陣列只顯示「100」,它說「量」:100 如果我使用:

$result["transaction"]["id"]; 

它將只顯示該ID,但我無法獲得顯示的金額。

+4

的[我如何從JSON中提取數據與PHP?(可能的複製https://stackoverflow.com/questions/29308898/how-做-I-提取物的數據從-JSON與 - PHP) –

回答

1

$result['transaction']['tenders'][0]['amount_money']['amount']

0

首先,你需要改變$result對象數組然後獲取數據

做這樣

$result=json_decode(json_encode($result,true),true); 

然後

$result['transaction']['tenders'][0]['amount_money']['amount'] 

我的事情會幫助你。

2

Use the following to check ur data and use last line like echo $data->tenders[0]->amount_money->amount; 
 
<?php 
 
$string='{ "id": "wt13LbKmJ2", "location_id": "EYGMFA", "created_at": "2017-07-01T07:32:57Z", "tenders": [ { "id": "C6xMF", "location_id": "MFA", "transaction_id": "NvDAOOA9", "created_at": "2017-07-01T07:32:57Z", "note": "Offering", "amount_money": { "amount": 100, "currency": "USD" }, "processing_fee_money": { "amount": 33, "currency": "USD" }, "customer_id": "14QEJXXM5TX", "type": "CARD", "card_details": { "status": "CAPTURED", "card": { "brand": "VI", "last_4": "0000" }, "entry_method": "ON_FILE" } } ], "product": "EXTERNAL_API" }'; 
 
$data=json_decode($string); 
 
echo '<pre>'; 
 
print_r(json_decode($string)); 
 
echo '</pre>'; 
 
echo $data->id.'<br>'; 
 
echo $data->tenders[0]->id; 
 
echo $data->tenders[0]->amount_money->amount; 
 

 
?>

強大的文本