Php 如何重新格式化此数组?

Php 如何重新格式化此数组?,php,arrays,Php,Arrays,所以我有这个数据库表 从那张表中,我想实现这一点(我的预期结果) 我不知道处理这个问题的最佳方法,所以现在我创建两个查询 我的第一个问题 SELECT * FROM ( select convert(created_date,date) created_date,code,'' code2,count(code) total, 0 total2 from morning_briefing where convert(created_date,

所以我有这个数据库表

从那张表中,我想实现这一点(我的预期结果)

我不知道处理这个问题的最佳方法,所以现在我创建两个查询

我的第一个问题

SELECT * FROM ( select convert(created_date,date) created_date,code,'' code2,count(code) total, 0 total2
            from morning_briefing
            where  convert(created_date,date) =  convert('" . $selected_date . "',date) and code IS NOT NULL
            group by code order by created_date desc) a order by total desc
我的第二个问题

SELECT * FROM ( select convert(created_date,date) created_date,''code,code2,0 total,count(code2) total2
            from morning_briefing
            where  convert(created_date,date) =  convert('" . $selected_date . "',date)
            and code2 IS NOT NULL
            group by code2 order by created_date desc) a order by total2 desc
下面是我查询的结果

Array
(
    [0] => stdClass Object
        (
            [created_date] => 2021-03-16
            [code] => AA
            [code2] => 
            [total] => 2
            [total2] => 0
        )

    [1] => stdClass Object
        (
            [created_date] => 2021-03-16
            [code] => AB
            [code2] => 
            [total] => 1
            [total2] => 0
        )

    [2] => stdClass Object
        (
            [created_date] => 2021-03-16
            [code] => BB
            [code2] => 
            [total] => 1
            [total2] => 0
        )

)
那么我的第二个查询结果是

Array
(
    [0] => stdClass Object
        (
            [created_date] => 2021-03-16
            [code] => 
            [code2] => AB
            [total] => 0
            [total2] => 2
        )

    [1] => stdClass Object
        (
            [created_date] => 2021-03-16
            [code] => 
            [code2] => AA
            [total] => 0
            [total2] => 1
        )

)
因此,我想合并这两个数组,然后我想排序
total列
desc,第二个顺序是
total2


顺便说一句,我使用的是mysql表。因此,我如何实现我的预期结果(如上所述),请提前感谢。

您可以循环使用
$results1
$result2
并创建一个包含预期结果格式数据的新数组

$total = max(count($result1), count($result2));
$formatted = [];

for($i = 0; $i < $total; $i++) {

    $formatted[] = [
        'code' => isset($result1[$i]) ? $result1[$i]->code : '-',
        'total' => isset($result1[$i]) ? $result1[$i]->total : 0,
        'code2' => isset($result2[$i]) ? $result2[$i]->code2: '-',
        'total2' => isset($result2[$i]) ? $result2[$i]->total2 : 0
    ];
}
$total=max(count($result1),count($result2));
$formatted=[];
对于($i=0;$i<$total;$i++){
$formatted[]=[
'code'=>isset($result1[$i])?$result1[$i]->代码:'-',
“总计”=>isset($result1[$i])?$result1[$i]->总计:0,
'code2'=>isset($result2[$i])?$result2[$i]->code2:'-',
'total2'=>isset($result2[$i])?$result2[$i]->total2:0
];
}

请解释如何从表中获得所需的结果,以及sql查询是什么样子的HI@AnuratChapanond,我更新了我的问题。现在可以了吗?这可能对您来说很明显,但我仍然不知道为什么代码字段从4个字符更改为2个字符。@AnuratChapanond,我更改了我的图像谢谢,现在它更有意义了:)