1

I have the following array:

Array
(
[aFlashParties] => Array
    (
        [2015-07-07] => 20
        [2015-07-08] => 48
        [2015-07-09] => 42
        [2015-07-10] => 94
        [2015-07-11] => 0
        [2015-07-12] => 0
        [2015-07-13] => 6
        [2015-07-14] => 0
    ),
[aRapidesParties] => Array
    (
        [2015-07-07] => 62
        [2015-07-08] => 6
        [2015-07-09] => 3
        [2015-07-10] => 17
        [2015-07-11] => 0
        [2015-07-12] => 0
        [2015-07-13] => 241
        [2015-07-14] => 0
    )
)

I'd like to turn it into an array like this:

Array
(
[0] => Array
    (
        [date]   => 2015-07-07
        [flash]  => 20
        [rapide] => 62
        [total]  => 82
    ),
[1] => Array
    (
        [date]   => 2015-07-08
        [flash]  => 48
        [rapide] => 6
        [total]  => 54
    ),
    .....
)

So the idea is to create an array which has date, flash (number from [aFlashParties]), rapid - umber from [aRapidesParties]) and total (sum of flash and rapid). Can you help me please?

Gareth Oakley
  • 1,020
  • 1
  • 12
  • 34
TanGio
  • 766
  • 2
  • 12
  • 34

2 Answers2

3

You just have to parse your arrays

$ret = [];
$keys = array_keys($a['aFlashParties']);
foreach ($keys as $key)
{
    $ret[] = [
        'date' => $key,
        'flash' => $a['aFlashParties'][$key],
        'rapide' => $a['aRapidesParties'][$key],
        'total' => $a['aFlashParties'][$key] + $a['aRapidesParties'][$key]
    ];
}

You may have to check the existence of some keys if there are not all dates in all arrays.

Mat
  • 2,134
  • 1
  • 18
  • 21
0

Using array_map()

Consider changing instance of $array with your input array's name,

$required = array_map(function($v1, $v2, $keys){
            return array('date'=>$keys, 'flash'=>$v1, 'rapide'=>$v2, 'total'=>$v1+$v2);
        }, $array['aFlashParties'], $array['aRapidesParties'], array_keys($array['aFlashParties']));
viral
  • 3,724
  • 1
  • 18
  • 32