如何打印特定项目的数组

时间:2018-09-13 作者:Sonjoy Datta

我使用PHP中的mysql查询从数据库中获得了以下数组。打印特定项目的方式是什么(例如:staff_namecoupon)?

{"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"}

2 个回复
最合适的回答,由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;
?>
结束

相关推荐

Wordpress Database Cleanup

重建WordPress数据库和更改网站的目录结构有多困难?我继承了两个网站的维护工作,我很确定目录结构设置不正确,数据库一团糟(两个网站有6个数据库?)。我是WordPress的新手,我怀疑现在使用现有网站重建网站会更容易。丢失数据没什么大不了的,网站也没什么特别的。该网站运行正常,但维护起来越来越困难。这是一个合理的计划吗?