2016-11-19 22 views
1

我的文字:在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":"[email protected]","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. 

回答

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":"[email protected]","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'}; 
+0

這是行得通的。但是,當我使用相同的過程來獲取地址等其他值時,它不會提供任何結果。你能告訴我如何獲取地址值嗎? –

+0

創建JSON對象後,您只需引用該JSON密鑰即可。看我的編輯。出於某種原因,您的地址碼是另一個JSON對象,因此您必須先引用註釋。 – TheValyreanGroup