php如何从数组中提取值

时间:2017-07-01 08:50:28

标签: php arrays

我有一个数组,我可以从我的结果中得到一些细节:

$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,但我无法显示该数量。

3 个答案:

答案 0 :(得分: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;

?>

强文

答案 1 :(得分:1)

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

答案 2 :(得分:0)

首先,您需要将$result 对象更改为数组然后获取数据

喜欢这个

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

然后

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

我会帮助你。