我需要简单的json in php from mysql,如下面给出的格式

时间:2014-01-20 08:14:42

标签: php json

我使用此代码对此代码进行了哪些更改

<?php
   $mysqli = new mysqli ( 'mysql2.000webhost.com', 'a8124593_local', 'root123', 'a8124593_test');
 // Check if album id is posted as GET parameter
   $myq = $mysqli ->query ( 'SELECT id, url, name FROM babyapp' );
  while ( $myr = $myq->fetch_assoc () ) {
   $array = (array("items"=>(array(
  'id' => $myr['id'],
  'name' => $myr['url'],
  'songs_count' => $myr['name'],
   ))));
  $arr_result[] = $array;
 }
 echo json_encode($arr_result);

&GT?; 结果:http://pixelerbricks.net78.net/youtubeapp/babyapp.php

但我的格式错误请帮助我,我希望得到这样的格式。我的结果代码有什么变化,如下格式

 { 
 "items": 
[
     {
     "id":1,
             "name":"China",
     "url":"http://www.androidbegin.com/tutorial/flag/china.png"
     }, 

     {
     "id":2,
             "name":"india",
     "url":"http://www.androidbegin.com/tutorial/flag/china.png"
     }
    ] 

}

1 个答案:

答案 0 :(得分:1)

<?php
   $mysqli = new mysqli ( 'mysql2.000webhost.com', 'a8124593_local', 'root123', 'a8124593_test');
 // Check if album id is posted as GET parameter
   $myq = $mysqli ->query ( 'SELECT id, url, name FROM babyapp' );
  while ( $myr = $myq->fetch_assoc () ) {
   $array["items"][] = (array(
  'id' => $myr['id'],
  'name' => $myr['url'],
  'songs_count' => $myr['name'],
   ));
 }
 echo json_encode($array);