Emjey23 Emjey23 - 5 days ago 5
JSON Question

How to generate a file format like this (javascript array??) ?? in PHP

Is this possible to generate a file format like below in PHP? Because I am gonna use this in my chart. So somebody please help me, any help will be appreciated.

[{
"title": "Marketing", /* This is a code from a sample in chart */
"value": 23, /* The sample chart of link is below */
"url":"#",
"description":"click to drill-down",
"data": [
{ "title": "Jan", "value": 1, "data": [
{ "title": "AAA", "value": 2 },
{ "title": "BBB", "value": 5 },
{ "title": "CCC", "value": 1 },
] },
{ "title": "Feb", "value": 2 },
{ "title": "Mar", "value": 1 },
{ "title": "Apr", "value": 3 },
{ "title": "May", "value": 2 },
{ "title": "Jun", "value": 1 },
{ "title": "Jul", "value": 2 },
{ "title": "Aug", "value": 3 },
{ "title": "Sep", "value": 3 },
{ "title": "Oct", "value": 1 },
{ "title": "Nov", "value": 1 },
{ "title": "Dec", "value": 3 }
]
}, {
"title": "Sales",
"value": 26,
"url":"#",
"description":"click to drill-down",
"data": [
{ "title": "Jan", "value": 4 },
{ "title": "Feb", "value": 3 },
{ "title": "Mar", "value": 1 },
{ "title": "Apr", "value": 4 },
{ "title": "May", "value": 2 },
{ "title": "Jun", "value": 1 },
{ "title": "Jul", "value": 2 },
{ "title": "Aug", "value": 2 },
{ "title": "Sep", "value": 3 },
{ "title": "Oct", "value": 1 },
{ "title": "Nov", "value": 1 },
{ "title": "Dec", "value": 3 }
]
}, {
"title": "Logistics",
"value": 30,
"url":"#",
"description":"click to drill-down",
"data": [
{ "title": "Jan", "value": 2 },
{ "title": "Feb", "value": 3 },
{ "title": "Mar", "value": 1 },
{ "title": "Apr", "value": 5 },
{ "title": "May", "value": 2 },
{ "title": "Jun", "value": 1 },
{ "title": "Jul", "value": 2 },
{ "title": "Aug", "value": 2 },
{ "title": "Sep", "value": 3 },
{ "title": "Oct", "value": 1 },
{ "title": "Nov", "value": 1 },
{ "title": "Dec", "value": 3 }
]
}];


Link of Sample Chart that ive been studying with..
http://codepen.io/team/amcharts/pen/c5ea20211ef3a5151480e88ed680dcb8

Thanks in advance..

Answer

You just need function json_encode() to format PHP Array become JSON like that. But make sure your PHP Installation has Json Extension (php-json)

$array = [
    [
        'title' => 'Marketing',
        'value' => 23,
        'url' => '#',
        'description' => 'click to drill-down',
        'data' => [
            [
                'title' => 'Jan',
                'value' => 1,
                'data'  => [
                    ['title' => 'AAA', 'value' => 2],
                    ['title' => 'BBB', 'value' => 5],
                    ['title' => 'CCC', 'value' => 1],
                ]
            ],
            [
                'title' => 'Feb',
                'value' => 2
            ],
            [
                'title' => 'Mar',
                'value' => 2
            ],
            [
                'title' => 'Apr',
                'value' => 2
            ],
            [
                'title' => 'May',
                'value' => 2
            ],
            [
                'title' => 'Jun',
                'value' => 2
            ],
            [
                'title' => 'Jul',
                'value' => 2
            ],
            [
                'title' => 'Aug',
                'value' => 2
            ],
            [
                'title' => 'Sep',
                'value' => 2
            ],
            [
                'title' => 'Oct',
                'value' => 2
            ],
            [
                'title' => 'Nov',
                'value' => 2
            ],
            [
                'title' => 'Des',
                'value' => 2
            ],
        ]
   ],
   [
        'title' => 'Logistic',
        'value' => 30,
        'url' => '#',
        'description' => 'click to drill-down',
        'data' => [
        ]

   ]
];
echo json_encode($array);
Comments