多维数组PHP-JSON

前端之家收集整理的这篇文章主要介绍了多维数组PHP-JSON前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何在 PHP中使用json_encode()创建一个具有以下结构的数组:
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);
原文链接:https://www.f2er.com/php/133109.html

猜你在找的PHP相关文章