score:2

Accepted answer

the problem is your query.

it should be like the following.

select x_axis, y_axis from yourtablename;

this way you'll get exactly the format that highcharts needs. you just have to insert it inside an array.

score:0

probably array combine and than json encode and replace? just in case for diversity;)

<?php 

$x_axis = array(
'safari', 'opera', 'firefox'
);

$y_axis = array(
10, 30.4, 50
);

$keyfilled = array_combine($x_axis, $y_axis);

arsort($keyfilled);

$jsondata = json_encode($keyfilled);

$jsondatareplaced = str_replace(array(',', '{', '}', ':'), array('],[', '[', ']', ','), $jsondata);
echo '<pre>';
var_dump('['.$jsondatareplaced.']');

?>

output is:

string(45) "[["firefox",50],["opera",30.4],["safari",10]]"

http://phpfiddle.org/main/code/jq4-cgb

score:1

[<? echo "'". json_encode($x_axis). "', " . json_encode($y_axis) ?>]

demo: http://codepad.org/g5jatxwu

nix that i was confused.

what you want to do is this:

foreach($x_axis as $key=>$x) {
    echo "['" . $x . "', " . $y_axis[$key] . "]";
}

demo: http://codepad.org/sknk1vax

to wrap it all up:

$d = array();
foreach($x_axis as $key=>$x) {
    $d[] = "['" . $x . "', " . $y_axis[$key] . "]";
}
echo json_encode($d);

demo: http://codepad.org/khofwxci

score:1

assuming:

$x_axis = array('safari', 'opera', 'firefox', 'ie', 'chrome', 'others');
$y_axis = array(10, 6, 40.5, 20, 10.6, 0.5);

this should work:

$data = array();
$length = count($x_axis);
for ($i = 0; $i < $length; $i++) {
    $data[] = array($x_axis[i], $y_axis[i]);
}

Related Query

More Query from same tag