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

How to Create a JSON Array in PHP?

Susan Sarandon
Susan SarandonOriginal
2024-12-01 02:15:10853browse

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"}
]

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);

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!

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