Home > front end >  php merging through multiple arrays
php merging through multiple arrays

Time:09-23

I am trying to read through the deskTickets array and accountArray to create a new array where it will add the desk ticket data to respective account array. An account can have multiple tickets, so I am trying to add those ticket array to respective account.

$deskTickets = array(
    array(
        "accountName"=> "Dame 1",
        "ticketNumber"=> "9782",   
        "status"=> "Open",
        "createdTime"=> "2022-08-30T00:39:16.000Z"
),
    array(
        "accountName"=> "Dame 1",
        "ticketNumber"=> "6723",   
        "status"=> "Open",
        "createdTime"=> "2022-08-30T00:39:16.000Z"
),
    array(
        "accountName"=> "Dame 2",
        "ticketNumber"=> "4567",   
        "status"=> "Open",
        "createdTime"=> "2022-08-30T00:39:16.000Z"
));


$accountArray=array(
                    "Dame 1"=> array(        
                        "Account_Name"=> "Dame 1",
                        "Store_Status"=> "Open",
                        "Helpdesk_Tickets"=>"No Tickets"
                    ),
                    "Dame 2"=> array(        
                        "Account_Name"=> "Dame 2",
                        "Store_Status"=> "Open",
                        "Helpdesk_Tickets"=>"No Tickets"
                    ));

Desired Output should be resulted to a finalArray. Something like below:

Array
(
    [Dame 1] => Array
        (
            [Account_Name] => Dame 1
            [Store_Status] => Open
            [Helpdesk_Tickets] => Array
                (
                    [0] => Array
                        (
                            [accountName] => Dame 1
                            [ticketNumber] => 9782
                            [status] => Open
                            [createdTime] => 2022-08-30T00:39:16.000Z
                        )

                    [1] => Array
                        (
                            [accountName] => Dame 1
                            [ticketNumber] => 6723
                            [status] => Open
                            [createdTime] => 2022-08-30T00:39:16.000Z
                        )

                )

        )

    [Dame 2] => Array
        (
            [Account_Name] => Dame 2
            [Store_Status] => Open
            [Helpdesk_Tickets] => Array
                (
                    [0] => Array
                        (
                            [accountName] => Dame 2
                            [ticketNumber] => 4567
                            [status] => Open
                            [createdTime] => 2022-08-30T00:39:16.000Z
                        )

                )

        )

)

CodePudding user response:

"group array of objects by property using reduce"


$deskTickets = array(
    array(
        "accountName" => "Dame 1",
        "ticketNumber" => "9782",
        "status" => "Open",
        "createdTime" => "2022-08-30T00:39:16.000Z",
    ),
    array(
        "accountName" => "Dame 1",
        "ticketNumber" => "6723",
        "status" => "Open",
        "createdTime" => "2022-08-30T00:39:16.000Z",
    ),
    array(
        "accountName" => "Dame 2",
        "ticketNumber" => "4567",
        "status" => "Open",
        "createdTime" => "2022-08-30T00:39:16.000Z",
    ));

$accountArray = array(
    "Dame 1" => array(
        "Account_Name" => "Dame 1",
        "Store_Status" => "Open",
        "Helpdesk_Tickets" => "No Tickets",
    ),
    "Dame 2" => array(
        "Account_Name" => "Dame 2",
        "Store_Status" => "Open",
        "Helpdesk_Tickets" => "No Tickets",
    ));

$grouped = array_reduce($deskTickets, function ($agg, $item) {
    $agg[$item["accountName"]][] = $item;
    return $agg;
}, []);

$result = array_map(function ($item) use ($grouped) {
    $item["Helpdesk_Tickets"] = $grouped[$item["Account_Name"]];
    return $item;
}, $accountArray);

print_r($result);

Output:

Array
(
    [Dame 1] => Array
        (
            [Account_Name] => Dame 1
            [Store_Status] => Open
            [Helpdesk_Tickets] => Array
                (
                    [0] => Array
                        (
                            [accountName] => Dame 1
                            [ticketNumber] => 9782
                            [status] => Open
                            [createdTime] => 2022-08-30T00:39:16.000Z
                        )

                    [1] => Array
                        (
                            [accountName] => Dame 1
                            [ticketNumber] => 6723
                            [status] => Open
                            [createdTime] => 2022-08-30T00:39:16.000Z
                        )

                )

        )

    [Dame 2] => Array
        (
            [Account_Name] => Dame 2
            [Store_Status] => Open
            [Helpdesk_Tickets] => Array
                (
                    [0] => Array
                        (
                            [accountName] => Dame 2
                            [ticketNumber] => 4567
                            [status] => Open
                            [createdTime] => 2022-08-30T00:39:16.000Z
                        )

                )

        )

)

CodePudding user response:

$mergedArray = [];

foreach($accountArray as $accountName => $data){
    $mergedArray[$accountName] = $data;
    $mergedArray[$accountName]['Helpdesk_Tickets'] = [];
    foreach($deskTickets as $ticket){
      if($ticket['accountName'] == $accountName){
         array_push($mergedArray[$accountName]['Helpdesk_Tickets'], $ticket);
      }
    }
}

print_r($mergedArray);

I hope code is self explanatory ...

CodePudding user response:

You can copy the account array and after that You should loop over the desk tickets to assign tickets to a proper account. For example:

$accountsWithTickets = $accountArray;

foreach ($deskTickets as $ticket) {
    if (!is_array($accountsWithTickets[$ticket["accountName"]]['Helpdesk_Tickets'])) {
        $accountsWithTickets[$ticket["accountName"]]['Helpdesk_Tickets'] = [];
    }
    
    $accountsWithTickets[$ticket["accountName"]]['Helpdesk_Tickets'][] = $ticket;
}

Also I would recommend to use classes for those structures instead of arrays.

  • Related