Home > Backend Development > PHP Tutorial > How to Convert a Multidimensional PHP Array to a JSON String?

How to Convert a Multidimensional PHP Array to a JSON String?

Linda Hamilton
Release: 2024-11-21 04:08:14
Original
331 people have browsed it

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;
?>
Copy after login

The output of the provided example will be:

[{"oV":"myfirstvalue","oT":"myfirsttext"},{"oV":"mysecondvalue","oT":"mysecondtext"}]
Copy after login

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!

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