Jump to content

[php] summing arrays


AndyPSV

Recommended Posts

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.