我有一个groups数组和成员数组。 Group_one可以是会员,管理员等
//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
)
...
现在我想计算Group_one,Group_two等中的数量。
我尝试了以下但没有工作。
foreach($groups as $group)
{
foreach ($members->result_array() as $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";
答案 0 :(得分:1)
这样的事情怎么样 -
$groupCount = array();
foreach($groups as $group){ $groupCount[$group['id']] = 0; }
foreach($members->result_array() as $member){
$groupCount[$member['group_id']]++;
}
print_r($groupCount);
group_id
的所有总数。group_id
参数增加该组的计数器。答案 1 :(得分:0)
可能这样的事情会起作用:
<?php
$groups = array(
array(
"id" => 1,
"name" => "Group one",
),
array(
"id" => 2,
"name" => "Group two",
),
array(
"id" => 3,
"name" => "Group three",
),
);
$members = array(
array(
"id" => 1,
"group" => "Group 1",
"group_id" => "1"
),
array(
"id" => 2,
"group" => "Group 1",
"group_id" => "1"
),
array(
"id" => 3,
"group" => "Group 2",
"group_id" => "2"
),
array(
"id" => 4,
"group" => "Group 2",
"group_id" => "2"
),
array(
"id" => 5,
"group" => "Group 2",
"group_id" => "2"
),
array(
"id" => 5,
"group" => "Group 3",
"group_id" => "3"
),
);
$count = array();
foreach($members as $member){
$count[$member['group_id']] = $count[$member['group_id']]+1;
}
var_dump($count);
?>
答案 2 :(得分:0)
您的论坛和会员的OOP结构如何?我明白了:
<?php
/**
* This cass describes a single group
*/
class Group {
private $id;
public $name;
private $members;
/**
* @param int $id
* @param string $name
*/
public function __construct($id, $name) {
$this->id = $id;
$this->name = $name;
}
/**
* Add a member to the list
* @param Member $member
*/
public function add_member(Member $member) {
$this->members[$member->get_ID()] = $member;
$member->associate_group($this);
}
/**
* Remove a member from the list
* @param Member $member
* @throws Exception in case of Member not existing.
*/
public function remove_member(Member $member) {
if (!in_array($member, $this->members, true)) {
throw new Exception("Member is not found in this group!");
}
unset($this->members[$member->get_ID()]);
}
public function count_members() {
return count($this->members);
}
}
class Member {
private $id;
private $group;
/**
* @param int $id
*/
public function __construct($id) {
$this->id = $id;
}
/**
* Associate this member with a group
* @param Group $group
*/
public function associate_group(Group $group) {
$this->group = $group;
}
public function get_group() {
return $this->group;
}
public function get_ID() {
return $this->id;
}
}
//Now to Action!
/** @var $groups Group[] */
$groups = array(
new Group(1, "Group One"),
new Group(2, "Group Two"),
new Group(3, "Group Three"),
new Group(4, "Group Four"),
);
$count = array();
//This would probably be called from within a loop iterating over database results
$groups[0]->add_member(new Member(1));
$groups[0]->add_member(new Member(2));
$groups[1]->add_member(new Member(3));
$groups[2]->add_member(new Member(4));
$groups[3]->add_member(new Member(5));
$groups[3]->add_member(new Member(6));
$groups[3]->add_member(new Member(7));
var_dump($groups);
//Good! Now all we need to do is count the members
foreach($groups as $group) {
$count[$group->name] = $group->count_members();
}
print_r($count);