Jump to content

Combine Array


everisk

Recommended Posts

I have an array that looks like

Array
(
    [1] => Array
        (
            [url] => http://www.abcde.com
            [stimes] => 823
            [pp] => 26.97
        )

    [2] => Array
        (
            [url] => http://www.12345.com
            [stimes] => 561
            [pp] => 18.38
        )

    [3] => Array
        (
            [url] => http://www.xyz123.com
            [stimes] => 1
            [pp] => 0.03
        )

    [4] => Array
        (
            [url] => http://www.xyz123.com
            [stimes] => 1
            [pp] => 0.03
        )

    [5] => Array
        (
            [url] => http://www.xyz123.com
            [stimes] => 1
            [pp] => 0.03
        )
)

 

How do I combine those with the same URL together? The [stimes] will also need to be accumulated.

 

Many thanks in advance.  :)

Link to comment
https://forums.phpfreaks.com/topic/86879-combine-array/
Share on other sites

try

<?php
$data = Array
(
    1 => Array
        (
            'url' => 'http://www.abcde.com',
            'stimes' => 823,
            'pp' => 26.97
        ),

    2 => Array
        (
            'url' => 'http://www.12345.com',
            'stimes' => 561,
            'pp' => 18.38
        ),

    3 => Array
        (
            'url' => 'http://www.xyz123.com',
            'stimes' => 1,
            'pp' => 0.03
        ),

    4 => Array
        (
            'url' => 'http://www.xyz123.com',
            'stimes' => 1,
            'pp' => 0.03
        ),

    5 => Array
        (
            'url' => 'http://www.xyz123.com',
            'stimes' => 1,
            'pp' => 0.03
        )
);

$combined = array();

foreach ($data as $item)
{
    if (isset($combined[$item['url']]['stimes'])) {
        $combined[$item['url']]['stimes'] += $item['stimes'];
    }
    else {
        $combined[$item['url']]['stimes'] = $item['stimes'];
    }
    $combined[$item['url']]['pp'] = $item['pp'];
}

/**
* view results
*/
echo '<pre>', print_r($combined, true), '</pre>';
?>

Link to comment
https://forums.phpfreaks.com/topic/86879-combine-array/#findComment-444184
Share on other sites

With the code, I now got array like

Array
(
    [http://www.abcde.com] => Array
        (
            [stimes] => 823
            [pp] => 26.97
        )

    [http://www.12345.com] => Array
        (
            [stimes] => 561
            [pp] => 18.38
        )

    [http://www.xyz123.com] => Array
        (
            [stimes] => 3
            [pp] => 0.03
        )

)

 

What do I need to change to get array like. I need to traverse array but number.

Array
(
    1 => Array
        (
            'url' => 'http://www.abcde.com',
            'stimes' => 823,
            'pp' => 26.97
        ),

    2 => Array
        (
            'url' => 'http://www.12345.com',
            'stimes' => 561,
            'pp' => 18.38
        ),

    3 => Array
        (
            'url' => 'http://www.xyz123.com',
            'stimes' => 3,
            'pp' => 0.03
        ),
);

 

Thanks a lot!

Link to comment
https://forums.phpfreaks.com/topic/86879-combine-array/#findComment-444274
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.