Unset array in multi dimensional array php -


i have multi dimensional array , try add sum of audience if have same division_id different post_page_id , return array total value of each division , unset rest. i've tried double foreach did not work. help? thanks

array (     [0] => array         (             [id] => 1             [post_page_id] => 22130             [audience] => 276             [type] => facebook             [division_id] => 1             [tw_audience] =>          )      [1] => array         (             [id] => 14             [post_page_id] => 22465             [audience] => 6             [type] => facebook             [division_id] => 1             [tw_audience] =>          )      [2] => array         (             [id] => 2             [post_page_id] => 22189             [audience] => 175             [type] => twitter             [division_id] => 2             [tw_audience] =>           )     [3] => array         (             [id] => 1             [post_page_id] => 23044             [audience] => 180             [type] => facebook             [division_id] => 2             [tw_audience] =>          ) ) 

so want output this

array (     [0] => array         (             [id] => 1             [post_page_id] => 22130, 22465             [audience] => 282             [type] => facebook             [division_id] => 1             [tw_audience] =>          )      [1] => array         (             [id] => 2             [post_page_id] => 22189, 23044             [audience] => 180             [type] => twitter+facebook             [division_id] => 2             [tw_audience] => 175          )  ) 

associative arrays friend.

$result = array (); foreach ($input $inp) {     $idx = $inp ['division_id'];     if  (isset ($result [$idx]))  //  new result, copy across     {         $result [$idx] = $inp;         //  since going have multiple pages, turn page_id array         $result [$idx]['post_page_id'] = array ($inp ['post_page_id']);     }     else  //  exists - add totals existing 1     {         $result [$idx]['audience'] += $inp ['audience'];         $result [$idx]['post_page_id'][] = $inp ['post_page_id'];     } } //  now, assume want rid of duplicates foreach ($result &$r) {     //  &$r means working pointer original, not copy     //  changes applied original     $r ['post_page_id'] = array_unique [$r ['post_page_id']];     //  , if want pages in comma-delimited format...     $r ['post_page_id'] = implode (', ', $r ['post_page_id']); } 

you might want same sources post_page_id implode them '+'.


Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -