🔴 Advanced  ·  Lesson 31

JSON Encode/Decode

JSON Encode/Decode

JSON in PHP

JSON is the standard format for APIs. PHP converts between arrays and JSON using json_encode() and json_decode().

Array to JSON (encode)

$data = ["name" => "Aman", "marks" => 88];
$json = json_encode($data);
echo $json;  // {"name":"Aman","marks":88}

JSON to Array (decode)

$json = '{"name":"Aman","marks":88}';
$arr = json_decode($json, true);   // true = associative array
echo $arr["name"];  // Aman

Summary

  • json_encode() turns an array into JSON (for sending).
  • json_decode($json, true) turns JSON into an array (for reading).

PHP में JSON

JSON APIs का standard format है। PHP arrays और JSON के बीच json_encode() और json_decode() से convert करता है।

Array से JSON (encode)

$data = ["name" => "Aman", "marks" => 88];
$json = json_encode($data);
echo $json;  // {"name":"Aman","marks":88}

JSON से Array (decode)

$json = '{"name":"Aman","marks":88}';
$arr = json_decode($json, true);   // true = associative array
echo $arr["name"];  // Aman

सारांश

  • json_encode() array को JSON बनाता है (भेजने के लिए)।
  • json_decode($json, true) JSON को array बनाता है (पढ़ने के लिए)।
← Back to PHP Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।