Create JSON object using PHP

前端 未结 5 650
没有蜡笔的小新
没有蜡笔的小新 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条回答
  •  萌比男神i
    2020-12-05 01:25

    Doing something like this should work if you would like to declare it as JSON only and not by using json_encode. This also eliminates the need to declare multiple variables for each of the arrays inside. But this would be a viable solution only if the contents of the array for data is finite.

    $json_string = '{ 
    "label": "Devices per year",
    "data": [
        [1999, 3.0], [2000, 3.9], [2001, 2.0], [2002, 1.2], [2003, 1.3], [2004, 2.5], [2005, 2.0], [2006, 3.1], [2007, 2.9], [2008, 0.9]
    ]}';
    

提交回复
热议问题