根据月PHP MYSQL组合JSON对象

Hi I am having problem generating a json object that will return branch, cf of that month under 1 array like this:

Desired Output

 [{
"month": "Jan",
"Branch2":  550965.96 ,
"Branch1": 663134.44
}, {
"month": "Feb",
"Branch2": 472793.10,
"Branch1": 492784.54
}, {
"month": "March",
"Branch2": 394616.65,
"Branch1": 757639.93  
}, {
"month": "April",
"Branch2": 376403.65 ,
"Branch1": 569404.61  
}]

Here is my current Code

<?php
  $dbhost = 'localhost';
  $dbname = 'spmdb';  
  $dbuser = 'root';                  
  $dbpass = ''; 


  try{

    $dbcon = new PDO("mysql:host={$dbhost};dbname={$dbname}",$dbuser,$dbpass);
    $dbcon->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

  }catch(PDOException $ex){

    die($ex->getMessage());
  }
$stmt = $dbcon->prepare("SELECT month.monthname,coll.branch,coll.cf FROM coll INNER JOIN month ON month.id = coll.`month` group by coll.month,coll.branch");
$stmt->execute();
$row1 = [];
while($row=$stmt->fetch(PDO::FETCH_ASSOC))
    {
        extract($row);
        $row1[]= ['month' => $monthname, $branch => $cf];

    }
    echo json_encode($row1,JSON_NUMERIC_CHECK);
?>

and currently ouputs this:

[{"month":"Jan","Branch4":40275}
,{"month":"Feb","Branch5":165173.91}
,{"month":"Feb","Branch1":93360.33}
,{"month":"Apr","Branch1":65415.75}]

Is there a way I could do this without modifying the mysql ?

It looks to me like you need to change the month placing in the loop/building of the array.

while($row=$stmt->fetch(PDO::FETCH_ASSOC))
{
     extract($row);
     $row1['month' => $monthname]= [$branch => $cf];

}

You have to use GROUP_CONCAT and CONCAT to your query

$stmt = $dbcon->prepare("SELECT month.monthname, GROUP_CONCAT(CONCAT(coll.branch,':',coll.cf)) as branchs FROM coll INNER JOIN month ON month.id = coll.`month` group by month.monthname");
$stmt->execute();
$row1 = [];
while($row=$stmt->fetch(PDO::FETCH_ASSOC))
{
    $branchs = $row['branchs'];
    $branchs = explode(",",$branchs);
    $branch_data = array();
    foreach($branchs as $branch)
    {
        $branch = explode(":", $branch);
        $branch_data[$branch[0]] = $branch[1];
    }       
    $row1[] = array_merge(array('name' => $row['monthname']), $branch_data);

}
echo json_encode($row1,JSON_NUMERIC_CHECK);

Check SQLFiddle: http://sqlfiddle.com/#!9/535f2/1