2012-07-07 34 views
3

我有一個組數組和成員數組。 Group_one可以是會員,管理等如何在PHP中使用另一個數組來計數數組?

//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"; 
+2

你可能想考慮一個OOP數據結構,IE,Group和Member都是通過字段鏈接的類。 – 2012-07-07 13:41:06

+0

包含您已擁有的數據和代碼非常棒。我減少了數據,只顯示了基本結構,因此代碼滾動的次數更少 - 如果您反駁隨意回滾我的編輯... – Lix 2012-07-07 13:51:57

回答

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. 然後,我檢查每個成員,並通過成員的參數group_id增加該組的計數器。
+0

請仔細檢查您是否在原始帖子中正確粘貼了代碼並且你正確地實現了代碼......這應該在你提供的數據給定的情況下工作...... – Lix 2012-07-07 13:49:13

+0

它仍然給出一個錯誤「未定義的索引:group_id」。我通過print_r更新了成員數組。 – shin 2012-07-07 13:54:40

+0

@shi - 由其他編輯和更新 – Lix 2012-07-07 13:58:43

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

怎麼樣一個面向對象的結構,爲您的組和成員?我得到了這個:

<?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); 
相關問題