I have this table containing data:
JSON result from query:
[{
"cnt": "1",
"category_name": "Entertainment",
"event_name": "Typhoon Sample",
"year_of_event": "2000"
}, {
"cnt": "1",
"category_name": "Heavy Rainfall Warning and Advisory",
"event_name": "Typhoon Abra",
"year_of_event": "2015"
}, {
"cnt": "1",
"category_name": "Daily Post",
"event_name": "No Event",
"year_of_event": " "
}, {
"cnt": "1",
"category_name": "Weather Forecast",
"event_name": "No Event",
"year_of_event": " "
}, {
"cnt": "2",
"category_name": "Actual Docs",
"event_name": "Holloween",
"year_of_event": "2018"
}, {
"cnt": "1",
"category_name": "Daily Post",
"event_name": "Holloween",
"year_of_event": "2018"
}]
I'm trying to transform it like this:
{
"category": ["Typhoon Sample 2000", "Typhoon Abra 2015", "No Event ", "Holloween 2018"],
"series": [{
"name": "Entertainment",
"data": ["1",0,0,0]
}, {
"name": "Heavy Rainfall Warning and Advisory",
"data": [0,"1",0,0]
}, {
"name": "Daily Post",
"data": [0,0,"1", "1"]
}, {
"name": "Weather Forecast",
"data": [0,0,"1",0]
}, {
"name": "Actual Docs",
"data": [0,0,0,"2"]
}]
}
What it does is, unique event_name is group then based on its position or index, the series properties will contain the name of each unique categories with the data from the query result. For example, if it does not have value fro Entertainment category, that event_name will have a value of 0.
What I've done so far:
while($row = $result_select->fetch_assoc()) {
$evennt = $row["event_name"]." ".$row["year_of_event"];
if (!in_array($evennt, $dbdata["category"])){
$dbdata["category"][]=$evennt;
}
$ccat = $row["category_name"];
$category_names = array_column($dbdata["series"], 'name');
if (!in_array($ccat, $category_names)){
$dbdata["series"][] = array(
'name' => $ccat,
'data' => []
);
}
print_r ($dbdata["series"]);
echo '<br/>';
print_r ($dbdata["category"]);
echo '<br/>';
echo 'Current '.$evennt.' --'.$ccat.'<br/>';
foreach (array_values($dbdata["series"]) as $i => $value) {
foreach (array_values($dbdata["category"]) as $ii => $valuee){
if(($value["name"] == $ccat) && ($valuee==$evennt ) && ($row["cnt"])){
array_push($dbdata["series"][$i]["data"],$row["cnt"]);
echo $ii.' : '.$value["name"].' : '.$valuee.' with value '.$row["cnt"].' <br/>';
}else if (in_array($evennt, $dbdata["category"]) && in_array($ccat, $category_names)){
array_push($dbdata["series"][$i]["data"],0);
echo $ii.' : '.$value["name"].' : '.$valuee.' without value 0 <br/>';
}
I'm stuck with the else statement.
Thanks.
Result from the code above:
{
"category": ["Typhoon Sample 2000", "Typhoon Abra 2015", "No Event ", "Holloween 2018"],
"series": [{
"name": "Entertainment",
"data": ["1", 0, 0, 0, 0]
}, {
"name": "Heavy Rainfall Warning and Advisory",
"data": ["1", 0, 0, 0, 0]
}, {
"name": "Daily Post",
"data": ["1", 0, 0, 0, "1"]
}, {
"name": "Weather Forecast",
"data": ["1", 0, 0, 0, 0]
}, {
"name": "Actual Docs",
"data": ["2", 0, 0, 0, 0]
}]
}
EDIT: I might have the wrong title, it's not actually transforming the JSON structure, my problem is how to manipulate the result of my query into the desired JSON structure. Also, The code snippet above does achieve the structure or format but the values in data is wrong.
json_encodeit will transform the array in to the format as you've mentioned.