简体   繁体   中英

Why my output in JSON data contain extra curly braces

The main problem of this code is that it provides extra curly braces ..

<?php   
header('Content-Type: json');
include('config.php');

    for($i=1990;$i<=2016;$i++){
        $sum=0;
        $data1=array();
        $result=mysql_query("select * from crimedetails  where crime_year=$i");
        while($row=mysql_fetch_array($result))
          {
            $sum+=$row['crime_mudered'];
            $data['crime_mudered']=$sum;
            $data['crime_year']=$row['crime_year'];
            }
          $data3[]=$data;
    }           
    array_push($data1,$data3);
    print json_encode($data1);
?>

Output data:

只需使用 array_merge() 而不是 array_push()

When you apply json_encode() to an associative array, the brackets become curly braces. That is because it is encoding that array to become a JavaScript Object Notation (JSON). So, in JavaScript, associative arrays are objects with properties . And, to define an object with properties in JavaScript, you use curly braces ( {} ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM