如何在
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);