phpstringjsonresponsemustache.php

What is this kind of data type and how can i call the data to display it individually


So I'm a newbie in php and i wanna ask what kind of data type is this? is it a stirng or a string array? How can i can them lets say if im only want to use the name variable in the string. How can i called them? If its in jsonresponse how do i do it also? If possible i wanted to use mustache template when displaying it. Basically i want to display each data in the string individually.

Here's the data that i received

string(1247) "[
{
"name": "POSLAJU NEXT DAY",
"service_code": "POSMY-PN-SDP",
"company_code": "POSMY",
"description": "Booking before 11.30am. \\nPickup and delivery in 1-2 days by PosLaju. \\nAvailable on working days days only. \\nNo booking in advance later than tomorrow. \\nYou can also book online and drop your document/parcel at the nearest POSLAJU branch. \\nOnly available on the web. \\nYou have to print the Consignment Note and follow our Packaging Guidelines.",
"instruction": "Please print the Consignment Note and follow our Packaging Guidelines.",
"payment_methods": [],
"cod_rate": 0,
"price": 6
 },
 {
  "name": "SAME DAY - MORNING",
  "service_code": "MDMY-SDD-S-MORNING-KV",
  "company_code": "MDMY",
  "description": "Booking before 8am. \\nPickup before 10am. \\nDelivery 
    before 2pm. \\nBy motorbike. \\nAvailable on working days only. 
   \\nPayment by credit/cash by sender/cash by receiver.",
  "instruction": "Please label your package with our Consignment Note or 
    Tracking No and follow our Packaging Guidelines.",
    "payment_methods": [
    {
      "code": "CASHPICKUP",
      "name": "Cash By Sender",
     "price": "3"
   }
 ],
   "cod_rate": 0.03,
  "price": 15
 }
 ]"

Here's the code in jsonresponse

   {"response":"[\n  {\n    \"name\": \"POSLAJU NEXT DAY\",\n    \"service_code\": \"POSMY-PN-SDP\",\n    \"company_code\": \"POSMY\",\n    \"description\": \"Booking before 11.30am. \\\\nPickup and delivery in 1-2 days by PosLaju. \\\\nAvailable on working days days only. \\\\nNo booking in advance later than tomorrow. \\\\nYou can also book online and drop your document\/parcel at the nearest POSLAJU branch. \\\\nOnly available on the web. \\\\nYou have to print the Consignment Note and follow our Packaging Guidelines.\",\n    \"instruction\": \"Please print the Consignment Note and follow our Packaging Guidelines.\",\n    \"payment_methods\": [],\n    \"cod_rate\": 0,\n    \"price\": 6\n  },\n  {\n    \"name\": \"SAME DAY - MORNING\",\n    \"service_code\": \"MDMY-SDD-S-MORNING-KV\",\n    \"company_code\": \"MDMY\",\n    \"description\": \"Booking before 8am. \\\\nPickup before 10am. \\\\nDelivery before 2pm. \\\\nBy motorbike. \\\\nAvailable on working days only. \\\\nPayment by credit\/cash by sender\/cash by receiver.\",\n    \"instruction\": \"Please label your package with our Consignment Note or Tracking No and follow our Packaging Guidelines.\",\n    \"payment_methods\": [\n      {\n        \"code\": \"CASHPICKUP\",\n        \"name\": \"Cash By Sender\",\n        \"price\": \"3\"\n      }\n    ],\n    \"cod_rate\": 0.03,\n    \"price\": 15\n  }\n]"}

Solution

  • Its called JSON (which stands for JavaScript Object Notation) which is a format used to share data between PHP and JavaScript (or any other supporting language).

    To use JSON data in PHP, you have to pass the string you got to json_decode. This will return the decoded data, which will be an array in your case. You can then access the data just like you can with a normal array. Just do a var_dump of the decoded string and you will see the data which it contains.