如果匹配,则使用PHP中另一个数组的值替换数组中的值。多维数组

时间:2015-11-02 16:36:09

标签: php arrays multidimensional-array

情景:

我有这两个数组

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];
    }
}

但看起来我迷失在这些多维阵列上并取而代之。任何帮助将非常感激。感谢。

2 个答案:

答案 0 :(得分:1)

根据我的理解你可以这样做: -

    $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);

答案 1 :(得分:0)

试试这个:

$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);