encode json using php?

前端 未结 5 1766
滥情空心
滥情空心 2020-12-06 07:33

I want to get json with php encode function like the following



        
5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-06 08:27

    Here is a solution that i came with

     $i = 1; 
     $array = array(); 
     while($row = mysql_fetch_array($result)) 
     { 
     $array[] = json_encode(array('id'=>$i, 'name' => $row['name'])); 
     $i++; 
     } 
     echo implode(',', $array); // Take output array glue it with the  
    

    This will put the json into an array and then implode it with glue (,) outputing the following {"results":[{"id":1,"name":"maadi"},{"id":2,"name":"monofiya"}]} without the need to do the array first then pass it to the json_encode() function

提交回复
热议问题