Jhonny Jr.
Jhonny Jr.

Reputation: 306

Merge 2 Multidimensional Array

I was try to call Google Analytics API on PHP using foreach loop like this:

foreach($getResults as $row) {
switch($row[0]) {
    case 'New Visitor':
        $data[] = array(
            'new' => $row[2],
            'date' => date("M d", strtotime($row[1]))
        );
    break;
    case 'Returning Visitor':
        $data[] = array(
            'return' => $row[2],
            'date' => date("M d", strtotime($row[1]))
        );
    break;
}}

And i have result array like this:

Array (
[0] => Array
    (
        [new] => 13
        [date] => Dec 17
    )

[1] => Array
    (
        [return] => 8
        [date] => Dec 17
    )

[2] => Array
    (
        [new] => 11
        [date] => Dec 16
    )

[3] => Array
    (
        [return] => 3
        [date] => Dec 16
    ) )

But i want to merge two each array above together, and will give an output like this:

Array (
[0] => Array
    (
        [new] => 13
        [return] => 8
        [total] => 21
        [date] => Dec 17
    )

[2] => Array
    (
        [new] => 11
        [return] => 3
        [total] => 14
        [date] => Dec 16
    ))

I has try to use array_merge() and using + operator to combine, but it didn't give me the result like what i excpected, anybody can help me?

Upvotes: 1

Views: 54

Answers (2)

The fourth bird
The fourth bird

Reputation: 163632

You could if you want use the date as an array key. Then for the 'total' key, you can first check if there already is a 'total' key. If there is not, add the first entry. If there already is one, then add to it.

If for example the $getResults looks like this:

$getResults = array(
    array(
        'New Visitor',
        '12/17/2015',
        13
    ),
    array(
        'Returning Visitor',
        '12/17/2015',
        8
    ),
    array(
        'New Visitor',
        '12/16/2015',
        11
    ),
    array(
        'Returning Visitor',
        '12/16/2015',
        3
    )
);

Maybe a setup like this can help you:

$data = array();

foreach ($getResults as $row) {
    $date = date("M d", strtotime($row[1]));
    isset($data[$date]['total']) ? $data[$date]['total'] += $row[2] : $data[$date]['total'] = $row[2];
    $data[$date]['date'] = $date;
    switch ($row[0]) {
        case 'New Visitor':
            $data[$date]['new'] = $row[2];
            break;
        case 'Returning Visitor':
            $data[$date]['return'] = $row[2];
            break;
    }
}

print_r($data);

// If you don't want the 'date' as a key, you can use:
// $data = array_values($data);

Will result in:

Array
(
    [Dec 17] => Array
        (
            [total] => 21
            [date] => Dec 17
            [new] => 13
            [return] => 8
        )

    [Dec 16] => Array
        (
            [total] => 14
            [date] => Dec 16
            [new] => 11
            [return] => 3
        )

)

Upvotes: 1

Grzegorz Gajda
Grzegorz Gajda

Reputation: 2474

You can use common key name:

$data['YOUR_COMMON_KEY'] = array(
    'new' => $row[2],
    'date' => 'Dec 17'
);

You can use a date value like a key. After iterating $getResults you should get a multidimensional array but without numeric keys. So if you would want to get numeric array, just use array_values().

Upvotes: 0

Related Questions