2017-07-01 46 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; 
 

 
?>

强大的文本