Arijit Aich Arijit Aich - 16 days ago 5
PHP Question

Fetch a specific variable value from a text in php

My Text:

//Stores DAta In Text Variable
$text = '{"id":"pay_6ixRYfJxUGVVB5","entity":"payment","amount":1100000,"currency":"INR","status":"authorized","order_id":null,"international":false,"method":"card","amount_refunded":0,"refund_status":null,"captured":false,"description":"Yamaha F310, 6-Strings Acoustic Guitar, Natural","card_id":"card_6ixRZh03lD6ch7","bank":null,"wallet":null,"vpa":null,"email":"a2@gmail.com","contact":"+917863495210","notes":{"address":"446\/3\/1, Lake Miami,
Calfiornia, USA.
Pincode: 121212. India."},"fee":null,"service_tax":null,"error_code":null,"error_description":null,"created_at":1479573225}';


How can I fetch The value of
amount
in
$amount
variable?

echo $amount variable.

Answer

You have a JSON string so use json_decode to get a JSON object.

$text = '{"id":"pay_6ixRYfJxUGVVB5","entity":"payment","amount":1100000,"currency":"INR","status":"authorized","order_id":null,"international":false,"method":"card","amount_refunded":0,"refund_status":null,"captured":false,"description":"Yamaha F310, 6-Strings Acoustic Guitar, Natural","card_id":"card_6ixRZh03lD6ch7","bank":null,"wallet":null,"vpa":null,"email":"a2@gmail.com","contact":"+917863495210","notes":{"address":"446\/3\/1, Lake Miami,
Calfiornia, USA.
Pincode: 121212. India."},"fee":null,"service_tax":null,"error_code":null,"error_description":null,"created_at":1479573225}';

$obj = json_decode($text);
echo $obj->{'amount'}; // 1100000
echo $obj->{'notes'}->{'address'};
Comments