Create JSON object using PHP

前端 未结 5 666
没有蜡笔的小新
没有蜡笔的小新 2020-12-05 00:40

How can I achieve or create this type JSON object using PHP?

{ 
    \"label\": \"Devices per year\",
    \"data\": [
        [1999, 3.0], [2000, 3.9], [200         


        
5条回答
  •  佛祖请我去吃肉
    2020-12-05 01:09

    I prefer the following syntax which gets the desired result and is clear to read:

    $ar = array(
                "label" => "Devices per years",
                "data" => array(array(1999, 3.0), array(2000, 3.9) )
            );
    
    var_dump(json_encode($ar));
    

    The only difference being that in the output "3.0" is rendered as "3". If you need the trailing ".0" you could surround those values with quotes.

提交回复
热议问题