3

I have an array which I was able to get some details from my results like this:

$result["transaction"];

when doing this the follow is displayed:

{ "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" }

How can I get my array to only display "100" where it say "amount":100 If I use:

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

it will display only the id but I can't get the amount to display.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Jerry Seigle
  • 417
  • 4
  • 12

3 Answers3

1

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

Cyril Graze
  • 3,881
  • 2
  • 21
  • 27
1

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;

?>
General Grievance
  • 4,555
  • 31
  • 31
  • 45
Jalin
  • 95
  • 7
0

First you need to change $result object to array then fetch the data

do like this

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

Then

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

I thing it will help you.

Bibhudatta Sahoo
  • 4,808
  • 2
  • 27
  • 51