Home  >  Article  >  Backend Development  >  How to convert array to json format in php

How to convert array to json format in php

青灯夜游
青灯夜游Original
2021-02-20 10:13:003439browse

In PHP, you can use the json_encode() function to convert an array into json format data. The syntax is "json_encode (array variable)". The json_encode() function can JSON encode variables, returning JSON data if successful, and FALSE if failed.

How to convert array to json format in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

json_encode() can be used in PHP Function to convert array to json format data.

<?php
   $arr = array(&#39;a&#39; => 1, &#39;b&#39; => 2, &#39;c&#39; => 3, &#39;d&#39; => 4, &#39;e&#39; => 5);
   echo json_encode($arr);
?>

The execution result of the above code is:

{"a":1,"b":2,"c":3,"d":4,"e":5}

JSON function

##json_encodeJSON encode the variablejson_decodeDecode the JSON format string and convert it into a PHP variablejson_last_errorReturn the last error that occurred
Function Description

json_encode

PHP json_encode() is used to JSON encode variables. This function returns JSON data if executed successfully, otherwise it returns FALSE.

Syntax

string json_encode ( $value [, $options = 0 ] )

Parameters

  • value: The value to encode. This function is only valid for UTF-8 encoded data.
  • options: Binary mask composed of the following constants: JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK, JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_FORCE_OBJECT
The following example demonstrates Learn how to convert PHP objects into JSON format data:

<?php
   class Emp {
       public $name = "";
       public $hobbies  = "";
       public $birthdate = "";
   }
   $e = new Emp();
   $e->name = "sachin";
   $e->hobbies  = "sports";
   $e->birthdate = date(&#39;m/d/Y h:i:s a&#39;, "8/5/1974 12:20:03 p");
   $e->birthdate = date(&#39;m/d/Y h:i:s a&#39;, strtotime("8/5/1974 12:20:03"));

   echo json_encode($e);
?>

The above code execution result is:

{"name":"sachin","hobbies":"sports","birthdate":"08\/05\/1974 12:20:03 pm"}

[Recommended learning: "

PHP Video Tutorial"]

json_decode

PHP json_decode() function is used to decode JSON format strings and convert them into PHP variables.

Syntax

mixed json_decode ($json_string [,$assoc = false [, $depth = 512 [, $options = 0 ]]])

Parameters

  • json_string: To be decoded JSON string, must be UTF-8 encoded data

  • assoc: When this parameter is TRUE, an array will be returned, and when FALSE, an object will be returned.

  • depth: A parameter of type integer that specifies the recursion depth

  • options: Binary Mask, currently only JSON_BIGINT_AS_STRING is supported.

Example

The following example demonstrates how to decode JSON data:

The execution result of the above code is:

object(stdClass)#1 (5) {
    ["a"] => int(1)
    ["b"] => int(2)
    ["c"] => int(3)
    ["d"] => int(4)
    ["e"] => int(5)
}

array(5) {
    ["a"] => int(1)
    ["b"] => int(2)
    ["c"] => int(3)
    ["d"] => int(4)
    ["e"] => int(5)
}

For more programming-related knowledge, please visit:

Programming Video! !

The above is the detailed content of How to convert array to json format in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:How to modify php versionNext article:How to modify php version