what is json_Encode and json_Decode function in PHP?

Posted by

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
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x