-1

I've got this array

Array
(
    [0] => Array
        (
            [Browser] => Chrome
            [Number_Browser] => 3
            [Platform] => MacOSX
        )

    [1] => Array
        (
            [Browser] => Default Browser
            [Number_Browser] => 10187
            [Platform] => unknown
        )

    [2] => Array
        (
            [Browser] => Chrome
            [Number_Browser] => 2
            [Platform] => MacOSX
        )

    [3] => Array
        (
            [Browser] => Chrome
            [Number_Browser] => 1
            [Platform] => MacOSX
        )

    [4] => Array
        (
            [Browser] => Chrome
            [Number_Browser] => 2
            [Platform] => MacOSX
        )

    [5] => Array
        (
            [Browser] => Chrome
            [Number_Browser] => 1
            [Platform] => MacOSX
        )

    [6] => Array
        (
            [Browser] => Chrome
            [Number_Browser] => 1
            [Platform] => MacOSX
        )

    [7] => Array
        (
            [Browser] => Default Browser
            [Number_Browser] => 1
            [Platform] => unknown
        )

    [8] => Array
        (
            [Browser] => Chrome
            [Number_Browser] => 4
            [Platform] => MacOSX
        )

    [9] => Array
        (
            [Browser] => Safari
            [Number_Browser] => 1
            [Platform] => MacOSX
        )

)

This is what I need to get as a result

Array
(
    [0] => Array
        (
            [Browser] => Chrome
            [Number_Browser] => 14
            [Platform] => MacOSX
        )

    [1] => Array
        (
            [Browser] => Default Browser
            [Number_Browser] => 10188
            [Platform] => unknown
        )

    [3] => Array
        (
            [Browser] => Safari
            [Number_Browser] => 1
            [Platform] => MacOSX
        )

)

I've been trying for a while with no luck. I need to combine the sub dimensions so when displaying the list on screen it displays the correct amount.

This data is coming from DB, which in Browser we get the User agent, so that's why there are a number of Chrome in the result because of different versions or User Agent string.

Then I'm using browser cap to make it easier to understand to the end user.

I would like then to combine the arrays so it give me the result as described above.

Any help would be appreciated.

Federico Giust
  • 1,803
  • 4
  • 20
  • 45
  • possible duplicate of [How to Flatten a Multidimensional Array?](http://stackoverflow.com/questions/1319903/how-to-flatten-a-multidimensional-array) – TecBrat Dec 12 '14 at 18:05
  • [Using `array_unique()` with multidimensional arrays](http://phpdevblog.niknovo.com/2009/01/using-array-unique-with-multidimensional-arrays.html) – slapyo Dec 12 '14 at 18:09
  • Not quite a duplicate, I trie the flatten methods described on that post but they do not give me the expected result. Not looking to flatten it but to combine the sub dimensions with the same key. – Federico Giust Dec 12 '14 at 18:16

1 Answers1

1

Can try using foreach(). Use an temporary array to set calculation results. Example:

$your_arr = Array
(
Array
(
    "Browser" => "Chrome",
    "Number_Browser" => 3,
    "Platform" => "MacOSX",
),

Array
(
    "Browser" => "Default Browser",
    "Number_Browser" => 3,
    "Platform" => "MacOSX",
),

Array
(
    "Browser" => "Chrome",
    "Number_Browser" => 3,
    "Platform" => "MacOSX",
),
//...............
);


$newArr = array();
foreach($your_arr as $key=>$val){
    $index = $val['Browser'];
    if(isset($newArr[$index])){
        $val_0 = $newArr[$index]['Number_Browser'] + $val['Number_Browser'];
        $newArr[$index] = array('Browser'=>$val['Browser'], "Number_Browser" => $val_0, 'Platform'=>$val['Platform']);
    }else{
        $newArr[$index] = $val;
    }
}
$result = array_values($newArr);

print '<pre>';
print_r($result);
print '</pre>';
MH2K9
  • 11,951
  • 7
  • 32
  • 49