php 将数组转化为json
php将数组转化为json的方法:首先创建一个PHP示例文件;然后定义一个数组;最后通过“json_encode”函数将指定数组转化为json即可。
本文操作环境:windows7系统、PHP7.1版,DELL G3电脑
数组转JSon数据:
$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}}}"
【推荐学习:PHP视频教程】