Home > Backend Development > PHP Tutorial > How to Create a JSON Array in PHP?

How to Create a JSON Array in PHP?

Susan Sarandon
Release: 2024-12-01 02:15:10
Original
852 people have browsed it

How to Create a JSON Array in PHP?

Creating an Array for JSON in PHP

This question arose when attempting to construct a JSON array within PHP code:

[
  {"region":"valore","price":"valore2"},
  {"region":"valore","price":"valore2"},
  {"region":"valore","price":"valore2"}
]
Copy after login

Solution

For this simple task, utilizing PHP's native json_encode function proves effective.

$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);

echo json_encode($arr);
Copy after login

This will seamlessly convert the PHP array into the desired JSON format.

For handling complex nested arrays, refer to the insightful post by andyrusterholz found on the PHP json_encode manual page.

The above is the detailed content of How to Create a JSON Array in PHP?. For more information, please follow other related articles on the PHP Chinese website!

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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template