PHP - merging 2D array by keys -


how can merge these array together?

 array     (         [0] => array             (                 [type] => person                 [relevance] => 0.700000                 [count] => 300                 [text] => chris             )     )   array     (         [0] => array             (                 [type] => person                 [relevance] => 0.900000                 [count] => 400                 [text] => chris             )          [1] => array             (                 [type] => person                 [relevance] => 0.500000                 [count] => 200                 [text] => tom             )     ) 

or array this:

array         (             [0] => array                 (                     [type] => person                     [relevance] => 0.700000                     [count] => 300                     [text] => chris                 )               [1] => array                 (                     [type] => person                     [relevance] => 0.900000                     [count] => 400                     [text] => chris                 )              [2] => array                 (                     [type] => person                     [relevance] => 0.500000                     [count] => 200                     [text] => tom                 )         ) 

the expected result is:

array (     [0] => array         (             [type] => person             [relevance] => 0.800000             [count] => 700             [text] => chris         )        [1] => array         (             [type] => person             [relevance] => 0.500000             [count] => 200             [text] => tom         ) ) 

[relevance] value average number

[count] value incremental number

the merging of these array should base on [text] value. how can php in fast way?

thanks helping.

if array

$array = array(     array(         'type' => 'person',         'relevance' => .7,         'count' => 300,         'text' => 'chris'     ),     array(         'type' => 'person',         'relevance' => .9,         'count' => 400,         'text' => 'chris'     ),     array(         'type' => 'person',         'relevance' => .5,         'count' => 200,         'text' => 'tom'     ), ); 

then:

$tmp = array();  foreach($array $obj) {     if(!isset($tmp[$obj['text']])) {         $tmp[$obj['text']] = array_merge(array('total_count'=>1),$obj);         continue;     }     $tmp[$obj['text']]['count']     += $obj['count'];     $tmp[$obj['text']]['relevance'] += $obj['relevance'];     $tmp[$obj['text']]['total_count']++; // useful average calculation }  $result = array();  foreach($tmp $key=>$obj) {     $obj['relevance'] = $obj['relevance']/$obj['total_count'];     unset($obj['total_count']); // useless     $result[] = $obj; }  print_r($result); 

Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -