我使用PHP中的mysql查询从数据库中获得了以下数组。打印特定项目的方式是什么(例如:staff_name
和coupon
)?
{"items":[{"ca_id":1,"appointment_date":"2018-07-02 08:00:00","service_name":"Eye check-up","service_price":50,"service_tax":0,"wait_listed":false,"deposit_format":null,"number_of_persons":"1","units":"1","duration":"3600","staff_name":"Mc Miltony","extras":[]}],"coupon":null,"subtotal":{"price":50,"deposit":0},"customer":"Mr Jhon","tax_in_price":"excluded","tax_paid":"5.00"}
最合适的回答,由SO网友:Jacob Peattie 整理而成
这不是WordPress的问题,但我会咬一口。
这些数据是JSON. 要使用PHP操作它,需要使用json_decode()
. 然后可以将其视为PHP对象。
为了访问您提到的值,假设JSON位于一个名为$json
:
$object = json_decode( $json );
$staff_name = $object->items[0]->staff_name;
$coupon = $object->coupon;
SO网友:David Corp
<?php
$results = \'{"items":[{"ca_id":1,"appointment_date":"2018-07-02 08:00:00","service_name":"Eye check-up","service_price":50,"service_tax":0,"wait_listed":false,"deposit_format":null,"number_of_persons":"1","units":"1","duration":"3600","staff_name":"Mc Miltony","extras":[]}],"coupon":null,"subtotal":{"price":50,"deposit":0},"customer":"Mr Jhon","tax_in_price":"excluded","tax_paid":"5.00"}\';
$object = json_decode( $results );
$staff_name = $object->items[0]->staff_name;
$coupon = $object->coupon;
?>