PHP添加具有相同日期的数组值

时间:2018-04-18 19:42:42

标签: php arrays

如果视图小于1,我想输出保持不变,但如果视图> 0我想添加共享相同日期的所有值并输出如下: -

Array
(
    [0] => Array
        (
            [date] => 09-04-2018
            [value] => 30
        )

    [1] => Array
        (
            [date] => 10-04-2018
            [value] => 32
        )

    [2] => Array
        (
            [date] => 11-04-2018
            [value] => 34
        )

    [3] => Array
        (
            [date] => 12-04-2018
            [value] => 36
        )

    [4] => Array
        (
            [date] => 13-04-2018
            [value] => 39
        )
)

我正在使用以下代码,该代码适用于view = 0但在view = 1时没有添加正确的值: -

$view = 1;

$a = array(
        array('name' => 'Days','date' => '09-04-2018','value' => '10'),
        array('name' => 'Nights','date' => '09-04-2018','value' => '20'),
        array('name' => 'Days','date' => '10-04-2018','value' => '11'),
        array('name' => 'Nights','date' => '10-04-2018','value' => '21'),
        array('name' => 'Days','date' => '11-04-2018','value' => '12'),
        array('name' => 'Nights','date' => '11-04-2018','value' => '22'),
        array('name' => 'Days','date' => '12-04-2018','value' => '13'),
        array('name' => 'Nights','date' => '12-04-2018','value' => '23'),
        array('name' => 'Days','date' => '13-04-2018','value' => '14'),
        array('name' => 'Days','date' => '13-04-2018','value' => '24'),
        array('name' => 'Days','date' => '13-04-2018','value' => '1')
    );

$i = 0;
foreach ($a as $b)
{
    $date = $b['date'];
    $value = $b['value'];

    $key = $i;

    if ($view > 0)
    {
        if ( $key = array_search($date, array_column($c, 'date')) > 0 )
        {
            $value = $c[$key]['value'] + $value;
        }
        else 
        {
            $key = $i;
        }
    }

    $c[$key]['name'] = 'Combined';
    $c[$key]['date'] = $date;
    $c[$key]['value'] = $value;

    $i++;
}

echo "<pre>";
print_r($c);
echo "</pre>";
谁能告诉我哪里出错了?

1 个答案:

答案 0 :(得分:1)

问题是您在$key = $i的情况下使用$view > 0,但$i可能大于$c的当前大小。然后,与array_keys的值的比较必须是严格的比较,检查密钥是否不同或大于0,或假(未找到)。最后,您需要创建一个空数组$c以开始避免array_column中的警告。

$view = 1;
$a = array(
        array('name' => 'Days','date' => '09-04-2018','value' => '10'),
        array('name' => 'Nights','date' => '09-04-2018','value' => '20'),
        array('name' => 'Days','date' => '10-04-2018','value' => '11'),
        array('name' => 'Nights','date' => '10-04-2018','value' => '21'),
        array('name' => 'Days','date' => '11-04-2018','value' => '12'),
        array('name' => 'Nights','date' => '11-04-2018','value' => '22'),
        array('name' => 'Days','date' => '12-04-2018','value' => '13'),
        array('name' => 'Nights','date' => '12-04-2018','value' => '23'),
        array('name' => 'Days','date' => '13-04-2018','value' => '14'),
        array('name' => 'Days','date' => '13-04-2018','value' => '24'),
        array('name' => 'Days','date' => '13-04-2018','value' => '1')
    );

$c =[];
foreach ($a as $i => $b)
{
    $date = $b['date'];
    $value = $b['value'];

    $key = $i;

    if ($view > 0)
    {
        $key = array_search($date, array_column($c, 'date'));
        if ($key !== false)
        {
            $value = $c[$key]['value'] + $value;
        }
        else
        {
            $key = count($c); // Create a new index here instead of $i
        }
    }
    else
    {
        $key = $i;
    }

    $c[$key]['name'] = 'Combined';
    $c[$key]['date'] = $date;
    $c[$key]['value'] = $value;
}

print_r($c);

输出:

Array
(
    [0] => Array
        (
            [name] => Combined
            [date] => 09-04-2018
            [value] => 30
        )

    [1] => Array
        (
            [name] => Combined
            [date] => 10-04-2018
            [value] => 32
        )

    [2] => Array
        (
            [name] => Combined
            [date] => 11-04-2018
            [value] => 34
        )

    [3] => Array
        (
            [name] => Combined
            [date] => 12-04-2018
            [value] => 36
        )

    [4] => Array
        (
            [name] => Combined
            [date] => 13-04-2018
            [value] => 39
        )

)