this is my JSON
{"status":1,"error":0,"packet_list":[{"booked_packet_id":"22597002","booking_date":"29/01/2020","track_number":"LE782095573","track_number_short":"782095573","booked_packet_weight":"500","arival_dispatch_weight":"947","booked_packet_vol_weight_w":"0","booked_packet_vol_weight_h":null,"booked_packet_vol_weight_l":null,"booked_packet_no_piece":"1","booked_packet_collect_amount":"0.00","booked_packet_order_id":"Clothes","origin_country_name":"PAKISTANr","origin_city_name":"LAHORE","destination_city_name":"KARACHI","invoice_number":"IBLE5027089","invoice_date":"2020-02-18","shipment_name_eng":"Printed Mobile Covers","shipment_email":"hello@covers.pk","shipment_phone":"03038518000","shipment_address":"HOUSE # 310 KAMRAN BLOCK ALLAMA IQBAL TOWN LAHORE","consignment_name_eng":"Umer Shabbir","consignment_email":null,"consignment_phone":"03312394595","consignment_phone_two":"0","consignment_phone_three":"0","consignment_address":"Ali Garh Bakery, PIB Colony near Jama Masjid Karachi","special_instructions":"Clothes Shahnaz Khala","booked_packet_status":"Delivered","status_remarks":"Array/Array","Tracking Detail":[{"Status":"Shipment picked in LAHORE MAIN CITY","Activity_Date":"2020-01-29","Activity_Time":"22:50:57","Activity_datetime":"2020-01-29 22:50:57"},{"Status":"Dispatched to KHI MAIN OFFICE","Activity_Date":"2020-01-29","Activity_Time":"23:52:36","Activity_datetime":"2020-01-29 23:52:36"},{"Status":"Arrived at Station in KARACHI LOCAL","Reciever_Name":null,"Activity_Date":"2020-01-30","Activity_Time":"13:59:00","Reason":null,"Activity_datetime":"2020-01-30 13:59:00"},{"Status":"Assigned to courier in KARACHI LOCAL","Reciever_Name":null,"Activity_Date":"2020-01-31","Activity_Time":"08:33:12","Reason":null,"Activity_datetime":"2020-01-31 08:33:12"},{"Status":"Delivered","Reciever_Name":"UMER","Activity_Date":"2020-01-31","Activity_Time":"10:44:33","Reason":"SELF","Activity_datetime":"2020-01-31 10:44:33"}]}]}
and this is my JSON beautifier result via an online tool
Here is my php code to echo data
foreach ($arr['packet_list'] as $a){ echo '<strong>Courier Name: </strong> Leopards Courier'; echo '<br>'; echo '<strong>Tracking #: </strong>' . $a['track_number']; echo '<br>'; echo '<strong>Current Status: </strong>' . $a['booked_packet_status']; echo '<br>'; echo '<br>'; echo '<strong>Booking Date: </strong>' . $a['booking_date']; echo '<br>'; echo '<strong>Customer Name: </strong>' . $a['consignment_name_eng']; echo '<br>'; echo '<strong>Destination City: </strong>' . $a['destination_city_name']; echo '<br>'; echo '<br>'; } foreach ($arr['Tracking Detail'] as $b){ echo '<strong>Status: </strong>' . $b['Status']; }
The ‘packet_list’ loop returns everything perfect but the error is ‘Tracking Detail’ did not return anything. I want to display all data available in a Tracking Detail. How to do this?
Thank You
Advertisement
Answer
Tracking Detail
isn’t located at $arr['Tracking Detail']
. It’s located at $arr['packet_list'][0,1,2...]['Tracking Detail']
, ie. it’s a sub-array under each packet’s array. For the simplest solution to output tracking details after each packet, using your code, nest the loops as follows:
foreach ($arr['packet_list'] as $a){ echo '<strong>Courier Name: </strong> Leopards Courier'; echo '<br>'; echo '<strong>Tracking #: </strong>' . $a['track_number']; echo '<br>'; echo '<strong>Current Status: </strong>' . $a['booked_packet_status']; echo '<br>'; echo '<br>'; echo '<strong>Booking Date: </strong>' . $a['booking_date']; echo '<br>'; echo '<strong>Customer Name: </strong>' . $a['consignment_name_eng']; echo '<br>'; echo '<strong>Destination City: </strong>' . $a['destination_city_name']; echo '<br>'; echo '<br>'; // Notice how we're using **$a**['Tracking Detail'] here. foreach ($a['Tracking Detail'] as $b){ echo '<strong>Status: </strong>' . $b['Status']; } }