Chartjs-Dynamically fill a json variable

1👍

Can you try this :

<?php

$data = array('L,100 C,61 ,31', 'L,50 C,39 ,97', ' L,40 C,59 ,67');
$result = array();
foreach($data as $key => $element){
    $parts = explode(" ",$element);
    foreach($parts as $part){
        $underParts = explode(',', $part);
        if(isset($underParts[0]) && isset($underParts[1]) ){
        $result[$underParts[0]][]=array('y'=>$underParts[1]);
        }


    }
}


var_dump(json_encode($result));

The result :

string(118) "{"L":[{"y":"100"},{"y":"50"},{"y":"40"}],"C":[{"y":"61"},{"y":"39"},{"y":"59"}],"":[{"y":"31"},{"y":"97"},{"y":"67"}]}"

Leave a comment