views:

67

answers:

1

I have a multidimensional array and am trying to group by a value of one the keys.

So

 Array (
[0] => Array  (
        [name] => Edward Foo
        [desc_topic] => Array (
                         [0] => Apple
                         [1] => Banana
                         [2] => Orange
                        )            
        [qtd_posts] => Array  (
                         [0] => 10
                         [1] => 20
                         [2] => 50
                        )

    )


[1] => Array  (
        [name] => Michael Max
        [desc_topic] => Array (
                         [0] => Apple
                         [1] => Banana
                         [2] => Orange
                        )  
        [qtd_posts] => Array  (
                         [0] => 10
                         [1] => 10
                         [2] => 10
                        )
    )

[2] => Array  (
        [name] => Edward Foo
        [desc_topic] => Array (
                         [0] => Apple
                         [1] => Banana
                         [2] => Orange
                        )            
        [qtd_posts] => Array  (
                         [0] => 5
                         [1] => 10
                         [2] => 30
                        )

    )

[3] => Array  (
        [name] => Michael Max
        [desc_topic] => Array (
                         [0] => Apple
                         [1] => Banana
                         [2] => Orange
                        )             
        [qtd_posts] => Array  (
                         [0] => 8
                         [1] => 8
                         [2] => 20
                        )

    )            

And I really need:

Array (
[0] => Array  (
        [name] => Edward Foo
        [desc_topic] => Array (
                         [0] => Apple
                         [1] => Banana
                         [2] => Orange
                        )            
        [qtd_posts] => Array  (
                         [0] => 15
                         [1] => 30
                         [2] => 80
                        )

    )

[1] => Array  (
        [name] => Michael Max
        [desc_topic] => Array (
                         [0] => Apple
                         [1] => Banana
                         [2] => Orange
                        )  
        [qtd_posts] => Array  (
                         [0] => 18
                         [1] => 18
                         [2] => 30
                        )

           )

  )

Thanks!!

A: 

I'm assuming the following:

  1. every name entry in the original array has an identical desc_topic sub-array (e.g. they all have the same Apple/Banana/Orange values for every instance.
  2. the qtd_posts sub-array has the to-be-grouped values in the same corresponding slots (e.g. all '1' entries are to be summed together, all '2' entries summed together, etc...)
  3. You want to preserve the parent array keys so that all 'Edward Foo' entries will use the first key used by an Edward Foo entry (e.g. 0)

If that applies, then something like this should work:

$newarr = array();
$reverse_map = array();

foreach($array as $idx => $entry) {
    if (isset($reverse_map[$entry['name']]) {
         // have we seen this name before? retrieve its original index value
         $idx = $reverse_map[$entry['name']]; 
    } else {
         // nope, new name, so store its index value
         $reverse_map[$entry['name']] = $idx;
    }

    // copy the 'constant' values
    $newarr[$idx]['name'] = $entry['name'];
    $newarr[$idx]['desc_top'] = $entry['desc_topic'];

    // sum the qtd_post values to whatever we previously stored.        
    foreach($entry['qtd_posts'] as $x => $y) {
        $newarr[$idx]['qtd_posts'][$x] += $y;
    }
}
Marc B
Thank you so much!!! You save my day!I was stuck with this code and didn't know how to solve this damm code.
Oliveira