Chris
Chris

Reputation: 1027

PHP add array values with same date

If view is less than one I would like to output to remain as it is but if view > 0 I would like to add all the values that share the same date and output it like:-

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

I am using the following code which works for view = 0 but does not add the correct values when 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>";

can anyone tell me where I am going wrong?

Upvotes: 1

Views: 612

Answers (1)

Syscall
Syscall

Reputation: 19780

The problem is that you use $key = $i in the case of $view > 0, but, $i could be greater than the current size of $c. Then, the comparison with the value of array_keys must be a strict comparison the check if the key if different or greater that 0, or false (not found). finally, you need to create an empty array $c to start to avoid a warning in 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);

outputs:

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
        )

)

Upvotes: 2

Related Questions