3

我有一个组数组和成员数组。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";
4

3 回答 3

1

像这样的东西怎么办——

$groupCount = array();
foreach($groups as $group){ $groupCount[$group['id']] = 0; }
foreach($members->result_array() as $member){
  $groupCount[$member['group_id']]++;
}

print_r($groupCount);
  1. 首先,我创建并初始化一个数组来保存每个group_id的所有总数。
  2. 然后我遍历每个成员,并通过membergroup_id的参数增加该组的计数器。
于 2012-07-07T13:31:24.630 回答
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);
?>
于 2012-07-07T13:44:20.420 回答
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);
于 2012-07-07T14:16:12.673 回答