-1

I have been trying to access particular parts of this Array - with no luck.

{"id":"ch_103axT2Yi0i8jQz2P2D7If2f","object":"charge","created":1393812736,
"livemode":false,"paid":true,"amount":2000,"currency":"usd","refunded":false,
"card":{"id":"card_103axT2Yi0i8jQz2tEhrmDqQ","object":"card","last4":"4242",
"type":"Visa","exp_month":12,"exp_year":2014,"fingerprint":"LcHz1FF8ePddYI7R",
"customer":"cus_3axTh2yyzxxGdh","country":"US","name":"Jason Wallace",
"address_line1":"","address_line2":"","address_city":"","address_state":" ",
"address_zip":"","address_country":"US","cvc_check":"pass",
"address_line1_check":"pass","address_zip_check":"pass"},"captured":true,
"refunds":[],"balance_transaction":"txn_103axT2Yi0i8jQz2BTUXNFnB",
"failure_message":null,"failure_code":null,"amount_refunded":0,
"customer":"cus_3axTh2yyzxxGdh","invoice":null,"description":null,
"dispute":null,"metadata":[]}

The particular fields I have been trying to access are [ name, email, address_line1, address_line2, address_city, address_state, address_zip, address_country ]. The Array is in object format, and I can't seem to get the syntax right. Any help would be appreciated. Thanks.

2 Answers2

0
$json = '{"id":"ch_103axT2Yi0i8jQz2P2D7If2f","object":"charge","created":1393812736,
"livemode":false,"paid":true,"amount":2000,"currency":"usd","refunded":false,
"card":{"id":"card_103axT2Yi0i8jQz2tEhrmDqQ","object":"card","last4":"4242",
"type":"Visa","exp_month":12,"exp_year":2014,"fingerprint":"LcHz1FF8ePddYI7R",
"customer":"cus_3axTh2yyzxxGdh","country":"US","name":"Jason Wallace",
"address_line1":"","address_line2":"","address_city":"","address_state":" ",
"address_zip":"","address_country":"US","cvc_check":"pass",
"address_line1_check":"pass","address_zip_check":"pass"},"captured":true,
"refunds":[],"balance_transaction":"txn_103axT2Yi0i8jQz2BTUXNFnB",
"failure_message":null,"failure_code":null,"amount_refunded":0,
"customer":"cus_3axTh2yyzxxGdh","invoice":null,"description":null,
"dispute":null,"metadata":[]}';
$json_decode = json_decode($json);
echo $json_decode->card->address_country;

See it in action

John Conde
  • 217,595
  • 99
  • 455
  • 496
0

First you need to decode the Json array with json_decode and you will be able to access the array like the following.

/* The Json encoded data */
$json = '{"id":"ch_103axT2Yi0i8jQz2P2D7If2f","object":"charge","created":1393812736,
"livemode":false,"paid":true,"amount":2000,"currency":"usd","refunded":false,
"card":{"id":"card_103axT2Yi0i8jQz2tEhrmDqQ","object":"card","last4":"4242",
"type":"Visa","exp_month":12,"exp_year":2014,"fingerprint":"LcHz1FF8ePddYI7R",
"customer":"cus_3axTh2yyzxxGdh","country":"US","name":"Jason Wallace",
"address_line1":"","address_line2":"","address_city":"","address_state":" ",
"address_zip":"","address_country":"US","cvc_check":"pass",
"address_line1_check":"pass","address_zip_check":"pass"},"captured":true,
"refunds":[],"balance_transaction":"txn_103axT2Yi0i8jQz2BTUXNFnB",
"failure_message":null,"failure_code":null,"amount_refunded":0,
"customer":"cus_3axTh2yyzxxGdh","invoice":null,"description":null,
"dispute":null,"metadata":[]}';

/* Json decode the encoded data */    
$data = json_decode($json, true);
$data = $data['card'];

/* Return the wanted values */
echo 'name: '            . $data['name'];
echo 'address line 1: '  . $data['address_line1'];
echo 'address line 2: '  . $data['address_line2'];
echo 'address city: '    . $data['address_city'];
echo 'address state: '   . $data['address_state'];
echo 'address zip: '     . $data['address_zip'];
echo 'address country: ' . $data['address_country'];
TURTLE
  • 3,728
  • 4
  • 49
  • 50