如何在php中格式化花括号数据

tjjdgumg  于 2023-01-16  发布在  PHP
关注(0)|答案(3)|浏览(172)

我有这种类型的数据存储在mysql中,我怎么能只回显金额值呢?
{"1":{"amount":"700","date":"2022-10-31","amount_discount":"0","amount_fine":"0","description":"","collected_by":"John Doe","payment_mode":"Cash","received_by":"12","inv_no":1},"2":{"amount":"300","date":"2022-12-17","description":" Paid by: Jane Doe","amount_discount":0,"amount_fine":"0","payment_mode":"upi","received_by":"23","inv_no":2}}
这段代码看起来像json数据,如果是的话,我应该使用jquery来回显这些值吗?或者有没有办法在php中实现?

vxbzzdmp

vxbzzdmp1#

您可以使用json_decode函数将其转换为PHP对象,然后访问其属性。对于您的示例,您可以执行以下操作以获取“amount”属性:

<?php
$js='{"1":{"amount":"700","date":"2022-10-31","amount_discount":"0","amount_fine":"0","description":"","collected_by":"John Doe","payment_mode":"Cash","received_by":"12","inv_no":1},"2":{"amount":"300","date":"2022-12-17","description":" Paid by: Jane Doe","amount_discount":0,"amount_fine":"0","payment_mode":"upi","received_by":"23","inv_no":2}}';
echo json_decode($js)->{"1"}->amount;
?>
bz4sfanl

bz4sfanl2#

$data = json_decode('{"1":{"amount":"700","date":"2022-10-31","amount_discount":"0","amount_fine":"0","description":"","collected_by":"John Doe","payment_mode":"Cash","received_by":"12","inv_no":1},"2":{"amount":"300","date":"2022-12-17","description":" Paid by: Jane Doe","amount_discount":0,"amount_fine":"0","payment_mode":"upi","received_by":"23","inv_no":2}}');
Ex1: ((array) $data)[1]->amount
Ex2: $data->{'1'}->amount]);
ffscu2ro

ffscu2ro3#

试试这个

$json_string = '{"1":{"amount":"700","date":"2022-10-31","amount_discount":"0","amount_fine":"0","description":"","collected_by":"John Doe","payment_mode":"Cash","received_by":"12","inv_no":1},"2":{"amount":"300","date":"2022-12-17","description":" Paid by: Jane Doe","amount_discount":0,"amount_fine":"0","payment_mode":"upi","received_by":"23","inv_no":2}}';

$json_data = json_decode($json_string, TRUE);

相关问题