从php中的文本中获取特定的变量值

时间:2016-11-19 16:43:03

标签: php

我的文字:

    //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}';

如何在amount变量中获取$amount的值?

echo $amount variable.

1 个答案:

答案 0 :(得分:0)

您有一个JSON字符串,因此请使用json_decode来获取JSON对象。

$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'};