AndyPSV Posted March 27, 2011 Share Posted March 27, 2011 I want to "count" (add 2 values) properly [sum "cnt" field]: It's a result of: $pm = array_merge($pm1,$pm2); array ( 0 => array ( 'id' => '3', 'id_' => '21', '_from' => '42', '_from_t' => '', '_to' => '1', 't' => 'Fabulous', 'date' => '2011-02-27 22:47:54', 'msg' => 'nnnn', 'read' => '1', 'cnt' => '1', ), 1 => array ( 'id' => '1', 'id_' => '20', '_from' => '42', '_from_t' => '', '_to' => '1', 't' => 'Convenience', 'date' => '2011-02-27 22:46:04', 'msg' => 'dd', 'read' => '1', 'cnt' => '2', ), 2 => array ( 'id' => '4', 'id_' => '21', '_from' => '42', '_from_t' => '', '_to' => '1', 't' => 'Fabulous', 'date' => '2011-02-27 22:48:24', 'msg' => 'no czesc co tam', 'read' => '1', 'cnt' => '2', '_temp' => '42', ), ) I WANT TO GET THIS (RESULT): array ( 0 => array ( 'id' => '3', 'id_' => '21', '_from' => '42', '_from_t' => '', '_to' => '1', 't' => 'Fabulous', 'date' => '2011-02-27 22:47:54', 'msg' => 'nnnn', 'read' => '1', 'cnt' => '3', ), 1 => array ( 'id' => '1', 'id_' => '20', '_from' => '42', '_from_t' => '', '_to' => '1', 't' => 'Convenience', 'date' => '2011-02-27 22:46:04', 'msg' => 'dd', 'read' => '1', 'cnt' => '2', ), How to do it? I was working with (sample) $pm = array_merge($pm1,$pm2); /* if(!empty($pm1)) { $pm_ = array_merge($pm1,$pm2); if(!empty($pm_)) { foreach($pm_ as $v) { if(!isset($pm[$v['t']])) { $pm[$v['t']] = $v; $pm[$v['t']]['cnt']++; echo $pm[$v['t']]['cnt'].'<br/>'; } } } $pm = array_values($pm); }*/ echo '<pre>';var_export($pm); Link to comment https://forums.phpfreaks.com/topic/231876-php-summing-arrays/ Share on other sites More sharing options...
AndyPSV Posted March 27, 2011 Author Share Posted March 27, 2011 did it if(!empty($pm1)) { $pm_ = array_merge($pm1,$pm2); if(!empty($pm_)) { foreach($pm_ as $v) if(!isset($pm[$v['t']])) $pm[$v['t']] = $v; else $pm[$v['t']]['cnt'] += $v['cnt']; } $pm = array_values($pm); } echo '<pre>';var_export($pm); Link to comment https://forums.phpfreaks.com/topic/231876-php-summing-arrays/#findComment-1192949 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.