PHP:将多个关联数组添加到另一个数组

时间:2022-08-26 14:07:23

Here's a sample of data

这是一个数据样本

Date  | Source | Amount
-----------------------
01A   | S1     | 12
01A   | S4     | 2
01A   | S7     | 134
02A   | S1     | 126
03A   | S4     | 10
02A   | S7     | 0
02A   | S1     | 3
02A   | S4     | 4
02A   | S7     | 5

The resulting array needs to look like:

生成的数组需要如下所示:

Array
(
    [01A] => Array
        (
            [S1] => 12
            [S4] => 2
            [S7] => 134
        )
    [02A] => Array
        (
            [S1] => 126
            [S4] => 10
            [S7] => 0
        )
    [03A] => Array
        (
            [S1] => 3
            [S4] => 4
            [S7] => 5
        )
)

I've tried looking at pages such as PHP Adding multiple associative arrays to new array based on key, PHP multiple arrays to one associative array and Merge multiple associative arrays to a single array of associative arrays and playing around with code as below (not using the same data - but same theory) but it's not producing what I need. The code below just shows A and B

我已经尝试过查看诸如PHP之类的页面将多个关联数组添加到基于键的新数组,将PHP多个数组添加到一个关联数组,并将多个关联数组合并到单个关联数组数组中,并使用如下代码进行操作(不使用相同的数据 - 但相同的理论)但它没有产生我需要的东西。下面的代码只显示A和B.

    $array = Array();
    $a=array( "ABC" => array("A"=>"RED","B"=>"GREEN"));
    $b=array( "ABC" => array("C"=>"BLUE","D"=>"YELLOW"));
    $array = $a + $b;
    echo '<pre>';
    print_r($array);
    echo '</pre>';

2 个解决方案

#1


1  

KISS

$result = array();
foreach($rows as $row)
{
    if(!isset($result[$row['Date']])) $result[$row['Date']] = array();
    $result[$row['Date']][$row['Source']] = $row['Amount'];
}

#2


1  

Assuming you already have the data in the $input array, the code is as easy as:

假设您已经在$ input数组中拥有数据,代码就像以下一样简单:

$result = array();
foreach ($input as $row) {
    $date = $row['Date'];
    if (! array_key_exists($date, $result)) {
        $result[$date] = array();
    }

    $result[$date][$row['Source']] = $row['Amount'];
}

If you don't have the data in $input but you fetch it from the database just replace the line:

如果您没有$ input中的数据但是从数据库中获取数据只需替换以下行:

foreach ($input as $row) {

with something that matches your data retrieval loop, f.e.:

与您的数据检索循环匹配的东西,f.e。:

while ($row = mysqli_fetch_assoc($result)) {

#1


1  

KISS

$result = array();
foreach($rows as $row)
{
    if(!isset($result[$row['Date']])) $result[$row['Date']] = array();
    $result[$row['Date']][$row['Source']] = $row['Amount'];
}

#2


1  

Assuming you already have the data in the $input array, the code is as easy as:

假设您已经在$ input数组中拥有数据,代码就像以下一样简单:

$result = array();
foreach ($input as $row) {
    $date = $row['Date'];
    if (! array_key_exists($date, $result)) {
        $result[$date] = array();
    }

    $result[$date][$row['Source']] = $row['Amount'];
}

If you don't have the data in $input but you fetch it from the database just replace the line:

如果您没有$ input中的数据但是从数据库中获取数据只需替换以下行:

foreach ($input as $row) {

with something that matches your data retrieval loop, f.e.:

与您的数据检索循环匹配的东西,f.e。:

while ($row = mysqli_fetch_assoc($result)) {