php convert array to json

藏色散人
Release: 2023-03-08 22:50:01
Original
3027 people have browsed it

php method to convert an array into json: first create a PHP sample file; then define an array; and finally convert the specified array into json through the "json_encode" function.

php convert array to json

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

Convert array to JSon data:

$array_1 = array(); //一维数组 $array_2 = array(); //多维数组 $array_1['username']='ericwolf'; $array_1['age']=25; $array_2['menber']['aa']['username']='ericwolf'; $array_2['menber']['aa']['age']=25; $array_2['menber']['bb']['username']='eeee'; $array_2['menber']['bb']['age']=22; print_r($array_2); $jsonObj_1 = json_encode($array_1); var_dump($jsonObj_1); $jsonObj_1 = json_encode($array_2); var_dump($jsonObj_1); 运行结果: Array ( [menber] => Array ( [aa] => Array ( [username] => ericwolf [age] => 25 ) [bb] => Array ( [username] => eeee [age] => 22 ) ) ) string(32) "{"username":"ericwolf","age":25}" string(84) "{"menber":{"aa":{"username":"ericwolf","age":25},"bb":{"username":"eeee","age":22}}}"
Copy after login

[Recommended learning:PHP video tutorial]

The above is the detailed content of php convert array to json. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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 Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!