i am running PHP Version 5.2.9, i want to create a barchart from data in mysql. i got this sumple and tried running it. am having a bit if trouble with json_encode(). i have an array that has numbers and strings but the numbers get printed back as strings. how do i make sure the numbers are printed back as nunmbers. the code i have is below. I ddnt update because the server i want to to use also uses php 5.2.* (000webhost)

$query = mysql_query("SELECT month, wordpress, codeigniter, highcharts FROM project_requests");

$category = array();
$category['name'] = 'Month';

$series1 = array();
$series1['name'] = 'Wordpress';

$series2 = array();
$series2['name'] = 'CodeIgniter';

$series3 = array();
$series3['name'] = 'Highcharts';

while($row = mysql_fetch_assoc($query)) {
    $category['data'][] = $row['month'];
    $series1['data'][] = $row['wordpress'];
    $series2['data'][] = $row['codeigniter'];
    $series3['data'][] = $row['highcharts'];   

$result = array();

print json_encode($result, JSON_NUMERIC_CHECK);

the above code says json_encode requires 1 parameter and 2 are given

print json_encode($result, JSON_NUMERIC_CHECK);

, when i remove the second parameter to get

print json_encode($result);

, the graph i want renders but no bars as the numbers are returned as strings.

Member Avatar


CHeck out intval or cast variable with (int)


$arr = array("12","13","14");
echo json_encode($arr);

echo "<br />";

$arr = array_map("intval",$arr);
echo json_encode($arr);


Was assuming int. You can do same with float. (float) and floatval

the suggestion does not work. my initial output with the second parameter removed from json_encode()

[{"name":"Month","data": ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"]},{"name":"Wordpress","data":["4","5","6","2","5","7","2","1","6","7","3","4"]},{"name":"CodeIgniter","data":["5","2","3","6","7","1","2","6","6","4","6","3"]},{"name":"Highcharts","data":["7","8","9","6","7","10","9","7","6","9","8","4"]}]
The values in the square brackets are strings, i want them to be numeric. when i do what you suggested i only get an out put like

the output i desire is every month with a bar

Member Avatar


Because your intvalling the whole arrays not the value array in question

Member Avatar


Are you storing the wordpress, codeigniter... as varchar?

while($row = mysql_fetch_assoc($query)) {
    $category['data'][] = $row['month'];
    $series1['data'][] = intval($row['wordpress']);
    $series2['data'][] = intval($row['codeigniter']);
    $series3['data'][] = intval($row['highcharts']);   

Should work though.