PHP - 如何合并数组内的数组或生成所需的数组

问题描述 投票:0回答:1

我有这个

$containers = [
    0 => [
        'info' => ['id' => 1, 'category' => 'МКД'],
        'formula' => ['tkoSum' => '12', 'chko' => '15']
    ],
    1 => [
        'info' => ['id' => 2, 'category' => 'Офис'],
        'formula' => ['tkoSum' => '62', 'chko' => '45']
    ]
];

但是我需要这个

$containers = [
    0 => [
        'id' => 1,
        'category' => 'МКД',
        'tkoSum' => '12',
        'chko' => '15'
    ],
    1 => [
        'id' => 2,
        'category' => 'Офис',
        'tkoSum' => '62',
        'chko' => '45'
    ]
];

如果您有任何想法如何以需要的形式提前生成此数组,这里是我如何获取第一个数组的代码

while ($row = $table->fetch_array()) {
    $formula = [
        'tkoSum' => $this->getTkoSum($row['id']),
        'chko' => $this->getChko($row['id'])
    ];
    $containers[] = ['info' => $row, 'formula' => $formula];
}
php arrays mysqli
1个回答
0
投票

那么,假设类别值来自 $row 中的关联数组,下面的代码会起作用吗?

    while ($row = $table->fetch_array()) {
    $containers[] = [$row['id'], $row['category'], $this->getTkoSum($row['id']), $this->getChko($row['id'])];
}
© www.soinside.com 2019 - 2024. All rights reserved.