Home > Article > Backend Development > How to Convert a Multidimensional PHP Array to a JSON String?
Generate JSON String from Multidimensional Array Data
In PHP, the json_encode() function can be utilized to convert multidimensional arrays into valid JSON strings. Below is an example:
<?php // Assuming the following array $array = array( array( 'oV' => 'myfirstvalue', 'oT' => 'myfirsttext', ), array( 'oV' => 'mysecondvalue', 'oT' => 'mysecondtext', ), ); // Encode the array to JSON $json = json_encode($array); // Output the JSON string echo $json; ?>
The output of the provided example will be:
[{"oV":"myfirstvalue","oT":"myfirsttext"},{"oV":"mysecondvalue","oT":"mysecondtext"}]
It's important to verify that the JSON string is valid. Proper JSON syntax requires double quotes around the object property names and string values.
The above is the detailed content of How to Convert a Multidimensional PHP Array to a JSON String?. For more information, please follow other related articles on the PHP Chinese website!