Php 如何在数组中组合相同的键值

Php 如何在数组中组合相同的键值,php,mysql,multidimensional-array,Php,Mysql,Multidimensional Array,我的数据库表包含以下数据 ____________________________________ |名称|开始日期|结束日期|课程| ==================================================== |约翰| 2015-01-01 | 2015-01-01 |紧急| |玛丽| 2015-01-12 | 2015-01-15 |重要| |彼得| 2015-01-19 | 2015-01-20 |重要| |约翰| 2015-01-26 | 2015-01-2

我的数据库表包含以下数据
____________________________________
|名称|开始日期|结束日期|课程|
====================================================
|约翰| 2015-01-01 | 2015-01-01 |紧急|
|玛丽| 2015-01-12 | 2015-01-15 |重要|
|彼得| 2015-01-19 | 2015-01-20 |重要|
|约翰| 2015-01-26 | 2015-01-28 |重要|
====================================================

这是我获取数据的sql代码

$sql="
SELECT `name`,`start_date`,`end_date`,`class`,
       TIMESTAMPDIFF(DAY,start_date,end_date)+1 AS no_days
     FROM `schedule`"

$result=mysql_query($sql);
     while($row=mysql_fetch_assoc($result)){
        $data[] = array(
             'label' => $row["name"],
             'start' => $row["start_date"],
             'no_days'=> $row["no_days"],
             'class' => $row["class"],
        );
     }


class Gantti{
    var $data = array();
    var $blocks = array();
    var $block = array();
    // the rest of gantti class code is for table format so i didnt include it in here
}


 function parse(){
   foreach($this->data as $d){
        $this->blocks[$d['label']][]=array(
             'label' => $d['label'],
             'start'=>$start=($d['start']),
             'no_days'=>$no_days=$d['no_days'],
             'class'=>$d['class']);
     }
  }
  var_dump($this->blocks);
在我转储$this->block之后,它给了我以下数组排列

 array()
    john=>
      array()
        0=>
          array()
            'label' => string 'john'
            'start' => string '2015-01-01'
            'no_days'=> string '1'
            'class' => string 'urgent'
        1=>
          array()
            'label' => string 'john'
            'start' => string '2015-01-26'
            'no_days'=> string '3'
            'class' => string 'important'
    mary=>
      array()
        0=>
          array()
            'label' => string 'mary'
            'start' => string '2015-01-12'
            'no_days'=> string '4'
            'class' => string 'important'
    peter=>
      array()
        0=>
          array()
            'label' => string 'peter'
            'start' => string '2015-01-19'
            'no_days'=> string '2'
            'class' => string 'important'
但我的预期输出是这样的

 array()
   0=>
      array()
          'label' => string 'john'
          'start' =>
              array()
                  0=> string '2015-01-01'
                  1=> string '2015-01-26'
          'no_days' =>
              array()
                  0=> string '1'
                  1=> string '3'
          'class' =>
              array()
                  0=> string 'urgent'
                  1=> string 'important'
   1=>
      array()
          'label' => string 'mary'
          'start' =>
              array()
                  0=> string '2015-01-12'
          'no_days' =>
              array()
                  0=> string '4'
          'class' =>
              array()
                  0=> string 'important'
   2=>
      array()
          'label' => string 'peter'
          'start' =>
              array()
                  0=> string '2015-01-19'
          'no_days' =>
              array()
                  0=> string '2'
          'class' =>
              array()
                  0=> string 'important'

如何组合相同的标签值以获得预期输出

通过修改
forloop

function parse()
{
    foreach($this->data as $d)
    {
        $this->raw_blocks[$d['label']]['label'] = $d['label'];
        $this->raw_blocks[$d['label']]['start'][] = $d['start'];
        $this->raw_blocks[$d['label']]['no_days'][] = $d['no_days'];
        $this->raw_blocks[$d['label']]['class'][] = $d['class'];
    }

    foreach($this->raw_blocks as $block)
    {
         $this->blocks[] = $block;
    }
}

你还在使用mysql函数,为什么?他们甚至已经弃用了一段时间,这意味着所有新代码都不应该使用它…@WadeShuler说来话长。。但在我得到这个问题的答案后,我将停止使用mysql…-)我会保留“标签”作为钥匙,并从那里开始工作。如果您想使用您在预期示例中所述的索引号,则只会使事情复杂化。为了支持您想要的示例,我认为您需要在构建中循环数组,以检查是否出现john,然后添加数据。使用john作为密钥将使代码更容易。谢谢您的帮助。。您提供的代码给出了输出,其中john执行了4次,mary执行了2次,peter执行了一次..没问题:)取决于输入。这是你提供的吗?你能输出结果吗?第一个数组看起来像这个数组()0=>数组()'label'=>string'john''start'=>数组()0=>string'2015-01-01''no_days'=>数组()0=>string'1''class=>数组()0=>string'emergency'下一个与我的预期输出类似,但它是1=>array()'label'=>string'john''start'=>array()的3倍0=>string'2015-01-01'1=>string'2015-01-26''no_days'=>array 0=>string'1'1=>string'3''class=>array()0=>string'emergency'1=>string'important'