PHP Counting inside an Array -


i want create list if in array add value +1.

current output

[1] => array     (         [source] => 397         [value] => 1     )  [2] => array     (         [source] => 397         [value] => 1     )  [3] => array     (         [source] => 1314         [value] => 1     ) 

what want achieve

[1] => array     (         [source] => 397         [value] => 2     )  [2] => array     (         [source] => 1314         [value] => 1     ) 

my current dulled down php

        foreach ($submissions $timefix) {                //start countng               $data = array(                     'source' => $timefix['parent']['id'],                     'value' => '1'               );                $datajson[] = $data;      }              print_r($datajson); 

simply use associated array:

$datajson = array();  foreach ($submissions $timefix) {     $id = $timefix['parent']['id'];      if (!isset($datajson[$id])) {         $datajson[$id] = array('source' => $id, 'value' => 1);     } else {         $datajson[$id]['value']++;     } }  $datajson = array_values($datajson); // reset keys - don't nessesarily need 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -