3

I have a groups array and members array. Group_one can be 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 I want to count how many in Group_one, Group_two etc.

I tried the following but not working.

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";
2
  • 2
    You may want to consider an OOP data structure, IE, Group and Member are classes, which are linked by a field. Commented Jul 7, 2012 at 13:41
  • It's great that you included the data and code you already have. I've cut down on the data to only show the basic structure so that there is less code to scroll through - If you disprove feel free to rollback my edits... Commented Jul 7, 2012 at 13:51

3 Answers 3

1

What about something like this -

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

print_r($groupCount);
  1. Firstly I'm creating and initializing an array to hold all the total numbers by each group_id of the group.
  2. Then I go over each member and increment the counter for that group by the group_id parameter of the member.
Sign up to request clarification or add additional context in comments.

4 Comments

Please double check that you pasted your code correctly in your original post and that you implemented the code correctly as well... This should work given the data you supplied...
It still give an error "Undefined index: group_id". I updated the members array by print_r.
Finally I got an array, Array ( [1] => 0 [2] => 2 [3] => 0 [4] => 3 [5] => 0 ... )Thanks for your patience.
No problem! Make sure to post complete code samples - the structure of your $menbers variable was what tripped us up. Happy coding!
0

Probably something like this will do the work:

<?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);
?>

Comments

0

How about an OOP structure for your Groups and Members? I got this:

<?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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.