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

时间:2015-05-27 07:10:29

标签: php mysql multidimensional-array

我的数据库表包含以下数据 ____________________________________
|名字| start_date | end_date |班级|
================================
|约翰| 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->块之后,它给了我以下数组

的排列
 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'

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

1 个答案:

答案 0 :(得分:1)

尝试通过修改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;
    }
}