How to count in array by using another array in PHP -


i have groups array , members array. group_one can member, admin etc

//groups: array ( [0] => array ( [id] => 1 ... [name] => group_one ... ) [1] => array ( [id] => 2 ... [name] => group_two ... ) ... 

//members: ci_db_mysql_result object ( [conn_id] => resource id #12 [result_id] => resource id #55 [result_array] => array ( [0] => array ( [id] => 2 ... [group] => group_2 [group_id] => 2 ) [1] => array ( [id] => 5 ... [group] => group_four [group_id] => 4 ) ... 

now want count how many in group_one, group_two etc.

i tried following not working.

foreach($groups $group) { foreach ($members->result_array() $key => $list) { if($group['id']==$list['group_id']) { $stack[] = array(); array_push($stack,$list); } } } echo "<pre>stack is:"; //print_r($stack); echo count($stack); echo"</pre>end of stack"; 

i appreciate help.

thanks in advance.

what -

$groupcount = array(); foreach($groups $group){ $groupcount[$group['id']] = 0; } foreach($members->result_array() $member){ $groupcount[$member['group_id']]++; } print_r($groupcount); 
  1. firstly i'm creating , initializing array hold total numbers each group_id of group.
  2. then go on each member , increment counter group group_id parameter of member.

Comments

Popular posts from this blog

javascript - backbone.js Collection.add() doesn't `construct` (`initialize`) an object -

php - Get uncommon values from two or more arrays -

Adding duplicate array rows in Php -