简体   繁体   中英

Array for subseries on highcharts Drilldown

I'm having some problems generating graphs with drilldown in Highcharts. I'm using Highcharts to render a pie with drilldown series. I need to transform this output json

Drilldownseries = [
    {
        "name": "LAZIO",
        "data": [["ROMA", 28]],
        "id": "LAZIO"
    },
    {
        "name": "LAZIO",
        "data": [["FROSINONE", 218]],
        "id": "LAZIO"
    },
    {
        "name": "LAZIO",
        "data": [["LATINA", 212]],
        "id": "LAZIO"
    },
    {
        "name": "TOSCANA",
        "data": [["FIRENZE", 2]],
        "id": "TOSCANA"
    },
    {
        "name": "TOSCANA",
        "data": [["LIVORNO", 5]],
        "id": "TOSCANA"
    },
    {
        "name": "TOSCANA",
        "data": [["PISA", 9]],
        "id": "TOSCANA"
    }
];

to

Drilldownseries = [
    {
        "name": "LAZIO",
        "data": [["ROMA", 28], ["FROSINONE", 218], ["LATINA", 212]],
        "id": "LAZIO"
    },
    {
        "name": "TOSCANA",
        "data": [["FIRENZE", 2], ["LIVORNO", 5], ["PISA", 9]],
        "id": "TOSCANA"
    }
];

This is the part of query that populates the array:

...
if($res3)
{
    $i = 0;

    while ($row = mysqli_fetch_array($res3, MYSQLI_ASSOC))
    {
        $row3[$i]["name"] = $row["name"];
        $row3[$i]["data"] = [[$row["subname"],$row["data"]]];
        $row3[$i]["id"] = $row["id"];            
        $i++;
    };

    $row3 = json_encode($row3,JSON_NUMERIC_CHECK);    
};

I'd prefer to extract the array with php well formed, but should be the same tranform the json.

PHP 7.2
Highcharts 6.1.1

for benefit of all, this is my solution. Code refined should be appreciated :) First removed square brackets in code below

        $row3[$i]["data"] = [$row["subname"],$row["data"]];

then create the new array so

$repl = array();
$i = 0;
foreach ($row3 as $value) {
    if (!isset($repl[$i]['id'])) {      
        $repl[$i]['id'] = $value['id'];
        $repl[$i]['name'] = $value['name'];
        $repl[$i]['data'] = [$value['data']];
    } elseif (($repl[$i]['id']) <>$value['id']) {
            $i++;
            $repl[$i]['id'] = $value['id'];
            $repl[$i]['name'] = $value['name'];
            $repl[$i]['data'] = [$value['data']];
    } else {
            array_push($repl[$i]['data'], $value['data']);
    }
}
$resultx = json_encode($repl,JSON_NUMERIC_CHECK);  

thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM