Jerry Seigle
Jerry Seigle

Reputation: 457

php how to extract values from an array

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.

Upvotes: 3

Views: 67

Answers (3)

Jalin
Jalin

Reputation: 95

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;

?>

Upvotes: 1

Bibhudatta Sahoo
Bibhudatta Sahoo

Reputation: 4904

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.

Upvotes: 0

Cyril Graze
Cyril Graze

Reputation: 3900

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

Upvotes: 1

Related Questions