简体   繁体   中英

How to combine each row of data together?

In PHP, I want to add each row together.
Here's a summarized example of my own database:

c(CorporateID,CompanyName)
o(CorporateID,OutletID)
os(OutletID,StaffName)

Data:

c TABLE:

CorporateID   CompanyName
-----------   -----------
1             A&B Company

o TABLE:

CorporateID   OutletID
-----------   --------
1             1
1             2
1             3

os TABLE:

OutletID   StaffName
--------   ---------
1          Jeannie
1          Zoey
1          Jasmine
2          James
2          Catherine
3          Ada

Part of the PHP

$conn = new mysqli(server, dbuser, dbpw, db);
$query = "
    SELECT o.*, c.*, os.* 
    FROM Outlet o, Company c, OutletStaff os
    WHERE c.CorporateID = o.CorporateID
    AND o.OutletID = os.Outlet_ID";
$result = $conn->query($query);

$outp = "[";
while($rs = $result->fetch_array(MYSQLI_ASSOC)) {
    if ($outp != "[") {$outp .= ",";}
    $outp .= '{"CompanyName":"'. $rs["CompanyName"].' - Outlet'. $rs["OutletID"].'",';
    $outp .= '"Worker":"'.$rs["StaffName"].'"}';
}
$outp .="]";

$conn->close();

echo($outp);

Output

[{"CompanyName":"A&B - Outlet1","Worker":"Jeannie"},
{"CompanyName":"A&B - Outlet1","Worker":"Zoey"},
{"CompanyName":"A&B - Outlet1","Worker":"Jasmine"},
{"CompanyName":"A&B - Outlet2","Worker":"James"},
{"CompanyName":"A&B - Outlet2","Worker":"Catherine"},
{"CompanyName":"A&B - Outlet3","Worker":"Ada"}]

As you can see, there is repetitive companyname , due to different worker in that outlet.

Here's what I expect:

[{"CompanyName":"A&B - Outlet1","Worker":"Jeannie, Zoey, Jasmine"},
{"CompanyName":"A&B - Outlet2","Worker":"James, Catherine"},
{"CompanyName":"A&B - Outlet3","Worker":"Ada"}]

Is it able to do it? I personally feel that there is something to do with line 13 of the PHP:

$outp .= '"Worker":"'.$rs["StaffName"].'"}';

Maybe I need to loop then put into a variable, then put into this output?
How can I achieve it?

Suppose your MySQL response looks like this;

$result = [
    [
        "company_name" => "ABC",
        "worker" => "Dipesh"
    ],
    [
        "company_name" => "CDE",
        "worker" => "Ramesh"
    ],
    [
        "company_name" => "CDE",
        "worker" => "Mukhesh"
    ]
];

You could simply make a associative array with your unique key as shown by the code snippet below.

$res = [];
foreach($result as $value){
 $company_name = $value['company_name'];
 if( isset($res[$company_name]) ){
    $res[$company_name] = array_merge($res[$company_name], [$value['worker']]);
 }else{
     $res[$company_name] = [$value['worker']]; 
 }
}

And then combine the keys and values to get the response you require easily using php implode function.

$result = [];
foreach($res as $key => $val){
   $result[] = [
     'company_name' => $key,
     'worker' => implode($val,",")
   ];   
}

And for JSON response you can simply call json_encode($result) to get the required output.

[
  {
    "company_name": "ABC",
    "worker": "Dipesh"
  },
  {
    "company_name": "CDE",
    "worker": "Ramesh,Mukhesh"
  }
]

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