search
HomeBackend DevelopmentPHP ProblemHow to convert PHP array data to json

In PHP, you can use the json_encode() function to convert array data into json. The json_encode() function can json encode variables. If the conversion is successful, it will return json data, otherwise it will return false.

How to convert PHP array data to json

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

Now when we need to useajaxHow to convert php arrays to json when interacting with the background.

The powerful PHP has provided built-in functions: json_encode() and json_decode(). It is easy to understand that json_encode() converts a PHP array into Json. On the contrary, json_decode() is to convert Json into a PHP array.

The specific form of Json is:

1. Object

The object is an unordered "'name/value' pair" gather. An object starts with "{" (left bracket) and ends with "}" (right bracket). Each "name" is followed by a ":" (colon); "name/value" pairs are separated by a "," (comma).

2. Array

An array is an ordered collection of values ​​(value). An array starts with "[" (left bracket) and ends with "]" (right bracket). Values ​​are separated by "," (comma).

Note: The two forms of objects and arrays are called differently in JS. Objects are called with "." and arrays are called with subscripts [0] and [1]. Also note that when passing a Json string, values ​​of type string must be enclosed in quotes.

Instance 1:

$array = array("name" => "Eric","age" => 23); 
echo json_encode($array);

The program will print out:

{“name”:”Eric”,”age”:23}

Instance 2:

$array = array(0 => "Eric", 1 => 23); 
echo json_encode($array);

The program will output:

["Eric",23]

It can be seen from the above two examples that the two calls in js are different. The keys of the PHP array are all numbers, then json_encode() returns Json in the form of an array, if the keys of the PHP array are all strings. Then json_encode() will return Json in the form of an object.

In fact, as long as there is a key in the form of a string in the key of the PHP array, then json_encode() will return Json in the form of an object. This is not correct. Because, although no errors will occur in the PHP code, if such a Json is passed to a JS function, JS will treat the Json as an object, and it is impossible for an object to use numbers as attribute names. In other words, JS does not know what this is: user.0.username (the middle is the number zero)

Recommended learning: php video tutorial

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.