php数组与不同类型的键合并

oknrviil  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(338)

是否有任何方法可以合并具有唯一名称的数组键,这样我就可以获得具有其类型和值的唯一键。我正在使用yii2和mysql
我在yii2中使用以下sql查询:

$data= $query->SELECT(["sum(no_of_pages) as pages_coded",'assigned_to','type_of_request','concat(firstname," ",lastname) as userfullname'])
->from('task')
->leftJoin('user','user.username = task.assigned_to')
->andWhere("sdlc_phase='Resolved'")
->orWhere("sdlc_phase='Semi_Resolved'");
$query->groupby(['assigned_to', 'type_of_request']);
//$query->->addGroupBy('assigned_to');
$query->orderby(['assigned_to'=>SORT_ASC]);
$data = $query->all();

sql查询的输出为

Array
(
    [0] => Array
        (
            [pages_coded] => 5
            [type_of_request] => Collector Tweak
            [userfullname] => Sam
        )
    [1] => Array
        (
            [pages_coded] => 5
            [type_of_request] => Collector Code
            [userfullname] => John
        )
    [2] => Array
        (
            [pages_coded] => 42
            [type_of_request] => Collector Tweak
            [userfullname] => John
        )
    [3] => Array
        (
            [pages_coded] => 37
            [type_of_request] => Clinical Tweak
            [userfullname] => Dona
        )
    [4] => Array
        (
            [pages_coded] => 35
            [type_of_request] => Collector Code
            [userfullname] => Dona
        )
    [5] => Array
        (
            [pages_coded] => 7
            [type_of_request] => Clinical Code
            [userfullname] => Ricky
        )
    [6] => Array
        (
            [pages_coded] => 50
            [type_of_request] => Clinical Tweak
            [userfullname] => Ricky
        )
    [7] => Array
        (
            [pages_coded] => 4
            [type_of_request] => Collector Code
            [userfullname] => Ricky
        )
    [8] => Array
        (
            [pages_coded] => 17
            [type_of_request] => Collector Tweak
            [userfullname] => Ricky
        )
)

然而,我想生成这样的输出,有没有得到这个

Array
(
    [Sam] => Array
        (
            [0] => Clinical Code
            [1] => 0
            [2] => Clinical Tweak
            [3] => 0
            [4] => Collector Code
            [5] => 0
            [6] => Collector Tweak
            [7] => 5
        )
    [John] => Array
        (
            [0] => Clinical Code
            [1] => 0
            [2] => Clinical Tweak
            [3] => 0
            [4] => Collector Code
            [5] => 5
            [6] => Collector Tweak
            [7] => 42
        )
    [Dona] => Array
        (
            [0] => Clinical Code
            [1] => 0
            [2] => Clinical Tweak
            [3] => 37
            [4] => Collector Code
            [5] => 0
            [6] => Collector Tweak
            [7] => 35
        )
   [Ricky] => Array
        (
            [0] => Clinical Code
            [1] => 7
            [2] => Clinical Tweak
            [3] => 50
            [4] => Collector Code
            [5] => 4
            [6] => Collector Tweak
            [7] => 17
        )
)

帮我解决这个问题。

z2acfund

z2acfund1#

对于您想要的输出,您必须通过创建另一个数组的结果进行循环。

$output = array();
foreach ($data as $row) {
    $output[$row['userfullname']][$row['type_of_request']] = $row['pages_coded'];
}

这样,您将得到如下数组:

$output = array(
    'Sam' => array(
        'Collector Tweak' => 5
    ),
);

如果找不到其他列,显然希望它们由0填充:

$categories = array(
    'Clinical Code' => 0,
    'Clinical Tweak' => 0,
    'Collector Code' => 0,
    'Collector Tweak' => 0,
);
foreach ($output as &$outputItem) {
    $outputItem = array_merge($categories, $outputItem);
}

根据定义,它更容易跟踪索引及其各自的值。
此处测试:https://3v4l.org/0k40q

相关问题