score:1

Accepted answer

Here is a working example of how you can format the PHP into JSON. This does not include the mysql part, but that should not be hard to figure out.

Updated 2017/10/12, corrected mistake with double data:

Updated 2017/10/16, corrected mistake without array push:

<?php
$males   = array();
$malepersons = array();
$females = array();
$femalepersons = array();
$result  = array();

$values = [[
    "name" => 'Anna',
    "height" => 175,
    "weight" => 53.4,
    "gender" => 'Female'
    ],[
    "name" => 'Dan',
    "height" => 185.1,
    "weight" => 90.4,
    "gender" => 'Male'
    ] ];

//Simplified for loop
foreach ($values as $row){
    $person = array();
    $person['name'] = $row['name'];
    $person['x'] = $row['height'];
    $person['y'] = $row['weight'];

    if($row['gender'] == 'Male'){
        array_push($malepersons, $person);        
    } elseif($row['gender'] == 'Female') {
        array_push($femalepersons, $person);
    }
}

//took these out of the for loop, only need to be set once
$males['color'] = 'black'; 
$males['name'] = 'Male';
$males['data'] = $malepersons;
$females['color'] = 'green';
$females['name'] = 'Female';
$females['data'] = $femalepersons;


array_push($result, $males);
array_push($result, $females);
print json_encode($result, JSON_NUMERIC_CHECK);
?>

This will give you JSON that looks like this:

[{
  "color": "black",
  "name": "Male",
  "data": [{
    "name": "Dan",
    "x": 185.1,
    "y": 90.4
  }]
}, {
  "color": "green",
  "name": "Female",
  "data": [{
    "name": "Anna",
    "x": 175,
    "y": 53.4
  }]
}]

Which is what highcharts wants as series input.

In your case, the $values array is actually the data returned by SQL, you just need to change the loop to loop what you want it to loop.

Added working example: https://ideone.com/RPp3DO

PHP DOCS on keyed arrays: http://php.net/manual/en/language.types.array.php


Related Query

More Query from same tag