php 基于共享列值合并和分组多个二维数组中的行,然后将另一列的数据推入子数组

fumotvh3  于 2022-12-21  发布在  PHP
关注(0)|答案(3)|浏览(156)

场景:

    • 我有这两个数组**:

array1

Array
(
[1] => Array
    (
        [label] => pending
        [fillColor] => #468847
        [data] => 50
    )

[2] => Array
    (
        [label] => dispatched
        [fillColor] => #6ecf70
        [data] => 10
    )

[3] => Array
    (
        [label] => delivered
        [fillColor] => #f89406
        [data] => 1
    )

[4] => Array
    (
        [label] => invoiced
        [fillColor] => #3a87ad
        [data] => 2
    )

)

array2

Array
(
[1] => Array
    (
        [label] => pending
        [fillColor] => #468847
        [data] => Array
            (
                [0] => 1
            )

    )

)
    • 我需要的结果是**
Array
(
[1] => Array
    (
        [label] => pending
        [fillColor] => #468847
        [data] => Array
            (
                [0] => 50
                [1] => 1
            )
    )

[2] => Array
    (
        [label] => dispatched
        [fillColor] => #6ecf70
        [data] => Array
            (
                [0] => 10
                [1] => 0
            )
    )

[3] => Array
    (
        [label] => delivered
        [fillColor] => #f89406
        [data] => Array
            (
                [0] => 1
                [1] => 0
            )
    )

[4] => Array
    (
        [label] => invoiced
        [fillColor] => #3a87ad
        [data] => Array
            (
                [0] => 2
                [1] => 0
            )
    )

)

只有4个标签:

  • pending
  • dispatched
  • delivered
  • invoiced
    • 请注意,这些数组只是一个示例。第一个数组可能根本没有值或只有2个值,而第二个数组有3个值或没有值。**

由于上面的限制,我考虑使用array_replace并使用一个名为

base_array = ["pending", "dispatched", "delivered", "invoiced"]

我已经尝试循环base_array,并尝试将array1array2匹配(如果标签存在)。

    • 基本上,如果键(即标签)不存在于array1array2中,则替换的值在结果数组中将为0。**

我试过了

foreach($base_array as $key => $value) {
    if(in_array($key, $array1[$key])) {
        $array[$key] = $array1[$key];
    }
}

但是看起来我在这些多维数组和替换上迷路了。任何帮助都将非常感激。谢谢。

mutmk8jj

mutmk8jj1#

从你的问题我所理解的你可以这样做:-

$array = array(
    '1' => Array
        (
        'label' => 'pending',
        'fillColor' => '#468847',
        'data' => '50'
    ),
    '2' => Array
        (
        'label' => 'dispatched',
        'fillColor' => '#6ecf70',
        'data' => '10'
    ),
    '3 ' => Array
        (
        'label' => 'delivered',
        'fillColor' => '#f89406',
        'data' => '1'
    ),
    '4' => Array
        (
        'label' => 'invoiced',
        'fillColor' => '#3a87ad',
        'data' => '2'
    ),
);

$array2 = array
    (
    '1' => Array
        (
        'label' => 'pending',
        'fillColor' => '#468847',
        'data' => array
            (
            '0' => '1'
        )
    )
);

$temp = array();
$i = 0;
foreach ($array as $key => $value) {

    $temp[$key]['label'] = $value['label'];
    $temp[$key]['fillColor'] = $value['fillColor'];

    foreach ($array2 as $key2 => $value2) {

        if ($value['fillColor'] == $value2['fillColor'] && $value['label'] == $value2['label']) {
            $temp[$key]['data'][] = $value['data'];
            if (isset($value2['data'][$i])) {
                $temp[$key]['data'][] = $value2['data'][$i];
            }
        } else {
            $temp[$key]['data'][] = $value['data'];
            if (!isset($value2['data'][$i])) {
                $temp[$key]['data'][] = 0;
            }
        }
        $i++;
    }
}

echo '<pre>';
print_r($temp);
3j86kqsm

3j86kqsm2#

试试这个:

$array1 = array(
    array(
        'label'     => 'pending',
        'fillColor' => '#468847',
        'data'      => '50'
    ),
    array(
        'label'     => 'dispatched',
        'fillColor' => '#468847',
        'data'      => '10'
    ),
    array(
        'label'     => 'delivered',
        'fillColor' => '#468847',
        'data'      => '8'
    ),
    array(
        'label'     => 'invoiced',
        'fillColor' => '#468847',
        'data'      => '5'
    )

);
$array2 = array(
    array(
        'label'     => 'pending',
        'fillColor' => '#468847',
        'data'      => array()
    ),
    array(
        'label'     => 'dispatched',
        'fillColor' => '#6ecf70',
        'data'      => array()
    ),
    array(
        'label'     => 'delivered',
        'fillColor' => '#f89406',
        'data'      => array()
    ),
    array(
        'label'     => 'invoiced',
        'fillColor' => '#3a87ad',
        'data'      => array()
    )
);

foreach ($array1 as $order) {
    foreach ($array2 as $key => $group) {
        if ($order['label'] == $group['label']) {
            array_push($array2[$key]['data'], $order['data']);
        }
    }
}

var_dump($array2);
9rbhqvlz

9rbhqvlz3#

宣告具有空data值的预设数据列数组。
将默认数组、第一个数组和第二个数组合并为一个数组。
迭代合并数组的行。
声明由label值标识的引用数组。在联接到其组的子数组之前,将遇到的每个data值显式转换为数组。
代码:(Demo

$defaults = [
    ['label' => 'pending', 'fillColor' => '#468847', 'data' => []],
    ['label' => 'dispatched', 'fillColor' => '#6ecf70', 'data' => []],
    ['label' => 'delivered', 'fillColor' => '#f89406', 'data' => []],
    ['label' => 'invoiced', 'fillColor' => '#3a87ad', 'data' => []],
];

$result = [];
foreach (array_merge($defaults, $array1, $array2) as $row) {
    $label = $row['label'];
    $row['data'] = (array) $row['data'];
    if (!isset($ref[$label])) {
        $ref[$label] = $row;
        $result[] = &$ref[$label];
    } else {
        $ref[$label]['data'] = array_merge(
            $ref[$label]['data'],
            $row['data']
        );
    }
}
var_export($result);

相关问题