How to convert array to json format in php

青灯夜游
Release: 2023-03-08 06:58:01
Original
3431 people have browsed it

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);
?>
Copy after login

The execution result of the above code is:

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

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
FunctionDescription

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 ] )
Copy after login

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);
?>
Copy after login

The above code execution result is:

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

[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 ]]])
Copy after login

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:

Copy after login

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)
}
Copy after login
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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template