php通过多个数组进行合并



我正在尝试读取deskTickets数组和accountArray,以创建一个新的数组,它将把deskTicket数据添加到相应的帐户数组中。一个帐户可以有多个票证,所以我试图将这些票证数组添加到相应的帐户中。

$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"
));

所需输出应生成finalArray。如下所示:

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
)
)
)
)

"使用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);

输出:

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
)
)
)
)
$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);

我希望代码是不言自明的。。。

您可以复制帐户数组,然后您应该在桌面票证上循环,将票证分配给适当的帐户。例如:

$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;
}

此外,我建议对这些结构使用类,而不是数组。

相关内容

  • 没有找到相关文章

最新更新