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?
$ccome from and what does it contain?$c).if ( $key = array_search($date, array_column($c, 'date')) > 0 )seems to me to be the problem. it does make$keytrue or false, right?