多维数组PHP-JSON [英] Multidimensional array PHP-JSON
本文介绍了多维数组PHP-JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在PHP中使用json_encode()创建一个数组,其结构如下:
How to create an array in PHP that with json_encode() becomes a thing with following structure:
Array(
[1] => Array(
[id] => 1
[data] => 45
)
[2] => Array(
[id] => 3
[data] => 54
)
);
推荐答案
尝试如下操作:
//initialize array
$myArray = array();
//set up the nested associative arrays using literal array notation
$firstArray = array("id" => 1, "data" => 45);
$secondArray = array("id" => 3, "data" => 54);
//push items onto main array with bracket notation (this will result in numbered indexes)
$myArray[] = $firstArray;
$myArray[] = $secondArray;
//convert to json
$json = json_encode($myArray);
这篇关于多维数组PHP-JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文