多维数组PHP-JSON

前端之家收集整理的这篇文章主要介绍了多维数组PHP-JSON前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何在 PHP中使用json_encode()创建一个具有以下结构的数组:
  1. Array(
  2. [1] => Array(
  3. [id] => 1
  4. [data] => 45
  5. )
  6. [2] => Array(
  7. [id] => 3
  8. [data] => 54
  9. )
  10. );
尝试这样的事情:
  1. //initialize array
  2. $myArray = array();
  3.  
  4. //set up the nested associative arrays using literal array notation
  5. $firstArray = array("id" => 1,"data" => 45);
  6. $secondArray = array("id" => 3,"data" => 54);
  7.  
  8. //push items onto main array with bracket notation (this will result in numbered indexes)
  9. $myArray[] = $firstArray;
  10. $myArray[] = $secondArray;
  11.  
  12. //convert to json
  13. $json = json_encode($myArray);

猜你在找的PHP相关文章