Home > Backend Development > PHP Problem > How to convert associative array to json data in php

How to convert associative array to json data in php

青灯夜游
Release: 2023-03-13 08:00:02
Original
2685 people have browsed it

In PHP, you can use the json_encode() function to convert associative arrays into json data. This function can JSON encode PHP variables (arrays, objects, etc.) and convert them into json format data; Syntax "json_encode($array)".

How to convert associative array to json data in php

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

In PHP, you can use the json_encode() function To convert associative array to json 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

Output result:

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

Description:

json_encode - JSON encoding of variables.

json_encode($value, $options, $depth)
Copy after login

Parameters:

  • value: The value to be encoded can be any data type except the resource type.

  • options: Binary mask consisting of the following constants

  • JSON_FORCE_OBJECT, JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_INVALID_UTF8_IGNORE, JSON_INVALID_UTF8_SUBSTITUTE , JSON_NUMERIC_CHECK, JSON_PARTIAL_OUTPUT_ON_ERROR, JSON_PRESERVE_ZERO_FRACTION, JSON_PRETTY_PRINT, JSON_UNESCAPED_LINE_TERMINATORS, JSON_UNESCAPED_SLASHES, JSON_UNESCAPED_UNICODE, JSON_THROW_ON_ERROR. For details about JSON constants, please refer to the JSON Constants page.

  • depth: Set the maximum depth. Must be greater than 0.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to convert associative array to json data 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