Home >Backend Development >PHP Tutorial >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!