DevTN
DevTN

Reputation: 593

how to group an array by multiple keys in PHP

I have an array in PHP as below :

$data = array
  (
  array("Ticket_id" => "239248",
        "Order_Level_Issue" => "Database update",
        "Geo" => "EMEA",
        "Line_No" => "10",
        "Line_Level_Issue" => "Qty verification"),
  array("Ticket_id" => "239248",
        "Order_Level_Issue" => "Database update",
        "Geo" => "EMEA",
        "Line_No" => "20",
        "Line_Level_Issue" => "Payment verification"),
  array("Ticket_id" => "239249",
        "Order_Level_Issue" => "Shipping company",
        "Geo" => "EMEA",
        "Line_No" => "20",
        "Line_Level_Issue" => "Missing address")

  );

I want to format $data array by grouping the subarrays by multiple keys. The result should be as below :

$dataGrouped = array
  (
  array("Ticket_id" => "239248",
        "Order_Level_Issue" => "Database update",
        "Geo" => "EMEA",
        "Lines" => ( 
        array(
        "Line_No" => "10",
        "Line_Level_Issue" => "Qty verification"),
        array(
        "Line_No" => "20",
        "Line_Level_Issue" => "Payment verification") ) ),

  array("Ticket_id" => "239249",
        "Order_Level_Issue" => "Shipping company",
        "Geo" => "EMEA",
        "Lines" => ( 
        array(
        "Line_No" => "20",
        "Line_Level_Issue" => "Missing address") ) )

  );

So I will group by Ticket_id, Order_Level_Issue, Geo then create a subarray Lines where I will store all the lines. At the end when I do echo json_encode($dataGrouped); I will get this format

"data": [
      {
            "Ticket_id": "239248",
            "Order_Level_Issue": "Database update",
            "Geo": "EMEA",
            "Items": [
            {"Line_No": "10",
            "Line_Level_Issue": "Qty verification"},
            {"Line_No": "20",
            "Line_Level_Issue": "Payment verification"} ]
        },

        {
            "Ticket_id": "239249",
            "Order_Level_Issue": "Shipping company",
            "Geo": "EMEA",
            "Items": [
            {"Line_No": "20",
            "Line_Level_Issue": "Missing address"} ]
        } ]

What I did is, I create a combined key. I concatenated Ticket_id, Order_Level_Issue and Geo and for each key I tried to group the rest in seperate array. But I am not getting the result I want. here is my code. Any suggestions please what I am missing ? Thank you.

$dataGrouped = [];

foreach($data as $data_key => $d) {

$group_key = $d['Ticket_id'].'_'.$d['Order_Level_Issue'].'_'.$d['Geo'];

if(!isset($dataGrouped[$data_key]))
$dataGrouped[$data_key] = [];
$dataGrouped[$group_key][$data_key] = $d; }

echo json_encode($dataGrouped);

Upvotes: 1

Views: 834

Answers (1)

Alberto Moro
Alberto Moro

Reputation: 1013

I write this code:

foreach ($data as $d) {
    $group_key = $d['Ticket_id'].'_'.$d['Order_Level_Issue'].'_'.$d['Geo'];
    if (!isset($dataGrouped[$group_key])) {
        $dataGrouped[$group_key] = $d;
        unset($dataGrouped[$group_key]["Line_No"]);
        unset($dataGrouped[$group_key]["Line_Level_Issue"]);
    }
    $dataGrouped[$group_key]['Items'][] = array(
        "Line_No" => $d['Line_No'],
        "Line_Level_Issue" => $d['Line_Level_Issue']
    );
}
echo json_encode(array_values($dataGrouped));

If the key does not exist, I insert it with array_push. I reset the array with array_values.

With this code Line_No could be messy, consider using ksort

Upvotes: 1

Related Questions