0

I have get the following array from my database using mysql query in PHP. What is way to print the specific item (e.g: staff_name and 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"}
  • Where is this data in your database? How does that JSON you posted relate to the database structure? What does this have to do with WordPress? Please take a moment to review the "How to Ask" page in our help center, and then consider filing an edit to improve your question. – bosco Sep 18 '18 at 15:55
  • This data came from a WordPress plugin. – Sonjoy Datta Sep 18 '18 at 16:03
2

Not really a WordPress question, but I'll bite.

That data is JSON. To manipulate it with PHP you need to decode it with json_decode(). Then you can treat it as a PHP object.

So to access the values you'd mention, and assuming the JSON is in a variable named $json:

$object = json_decode( $json );
$staff_name = $object->items[0]->staff_name;
$coupon = $object->coupon;
  • I was little bit confused about the array. But, thanks a lot. – Sonjoy Datta Sep 13 '18 at 11:17
-3
<?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;
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.