php 合并 2 个数组时出现 ksort 问题

php ksort trouble on 2 arrays merged

我对 ksort 函数有疑问:

我创建数组:

foreach ($json2['ticket']['records'] as $item) {
    if ($item['externalId1'] == "325") {
        $subject = $item['subject'];
        $fields1[] = [ //this array must be created dynamic
            $item['insertDate'] => $subject . 'ticket',
        ];

    }
}

$query1204 = 'SELECT * FROM interventions where inter_clientid = "325"';
$result1204 = $db->query($query1204) or die($db->error);
while ($row1204 = $result1204->fetch_array()) {
    $fields2[] = [
        strtotime($row1204['inter_datedebprev']) * 1000 =>
            $row1204['inter_title'] . 'INTER',
    ];
}

然后我对它的合并版本进行排序

$final = array_merge($fields1,$fields2);
ksort($final);
$fild = json_encode($final);
print_r($fild);

事实是数组是分开排序的:

[
{
1559671503000: "Migration téléphonieticket"
 },
{
1559831744000: "Maintenanceticket"
},
{
1561723413000: "Renouvellement de postesticket"
},
{
1561743016000: "Migration vers Office 365ticket"
},
{
1562164271000: "ABO Office 365ticket"
},
{
1564221684000: "sdsfsdfticket"
},
{
1564728016000: "sdfsfticket"
},
{
 1564129800000: "Migration vers Office 365INTER"
},
{
 1564043400000: "Renouvellement de postesINTER"
 },
 {
 1564475400000: "Installation poste Maison AlfortINTER"
 },
 {
 1564644600000: "Installation copieur Maison AlfortINTER"
 }

有什么问题

排序功能应该同时应用于所有数据,而不是每个块

谢谢!

函数 ksort 将对数组的键进行排序,因为您的数组是多维的,它正在对数字索引进行排序(即什么都不做)。您将需要比较 each 数组的键,为此您可以使用 usort (我假设您只想按日期订购所有项目):

usort($final, function($a, $b) { return array_keys($a)[0] - array_keys($b)[0]; });