Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!
        We spend hours scrolling social media and waste money on things we forget, but won’t spend 30 minutes a day earning certifications that can change our lives.
        
Master in DevOps, SRE, DevSecOps & MLOps by DevOps School!
    
Learn from Guru Rajesh Kumar and double your salary in just one year.
json_Encode() Function
 The json_encode() function is an inbuilt function in PHP which is used to convert PHP array into JSON representation, this can return a JSON encoded string on success or false on failure.

syntax
string json_encode( $value )example
<?php
   $post_data = array(
      "item" => array(
            "item_type_id" => 1,
            "tring_key" => "AA",
            "string_value" => "Hello",   
            "string_extra" => "App",
            "is_public" => 1,
            "is_public_for_contacts" => 0
      )
   );
   echo json_encode($post_data)."\n";
?>output
{"item":{"item_type_id":1,"tring_key":"AA","string_value":"Hello","string_extra":"App","is_public":1,"is_public_for_contacts":0}}json_Decode() function
The json_decode() function is an inbuilt function in PHP which is used to decode a JSON string. It converts a JSON encoded string into a PHP variable.

syntax
json_decode( $json, $assoc = FALSE )example
<?php
  
$data = '{"g":7, "e":5, "e":5, "k":11, "s":19}';
  
var_dump(json_decode($data, true));
  
?>output
array (size=4)
  'g' => int 7
  'e' => int 5
  'k' => int 11
  's' => int 19
Leave a Reply