What is the function to convert php array to json?

青灯夜游
Release: 2023-03-09 08:26:02
Original
1781 people have browsed it

The function for converting php array to json is json_encode(). The json_encode() function can JSON encode variables and convert them into json string data with the syntax format "json_encode (value)".

What is the function to convert php array to json?

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

How to convert php array to json? You can use the json_encode() function.

Example:

<?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

Output:

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

PHP json_encode() function

json_encode() function can Variables are JSON encoded

Syntax:

json_encode ( $value [, $options = 0 ] )
Copy after login

Returns a string containing the representation of value in JSON form.

Note:

1. $value is the value to be encoded, and this function is only valid for UTF8 encoded data;

2. options: consists of the following constants Binary masks: JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK, JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_FORCE_OBJECT;

3. The second parameter is generally not needed;

4. JSON data is actually a string , you can use var_dump() to print it out to see the data type;

5. If the execution is successful, JSON data will be returned, otherwise FALSE will be returned.

Return value:

  • Returns a JSON encoded string on success or false on failure.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What is the function to convert php array to json?. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!