1

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);
1
  • 1
    Does the data come from database? If so consider grouping in your query before it goes to php Commented Feb 12, 2020 at 14:27

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.