LBMG
LBMG

Reputation: 73

how to combine same key value in an array

my database table contained the folowing data
____________________________________
|name | start_date  | end_date      | class       |
================================
|john   | 2015-01-01 | 2015-01-01  | urgent     |
|mary | 2015-01-12 | 2015-01-15  | important |
|peter | 2015-01-19 | 2015-01-20  | important |
|john  | 2015-01-26 | 2015-01-28   | important |
=================================

and these is my sql code in fetching the data

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

after i dump $this->block, it gave me the following arrangement of array

 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'

but my intended output is like this

 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'

How can i combine same label value to get my intended output?

Upvotes: 2

Views: 162

Answers (1)

sitilge
sitilge

Reputation: 3737

Try to rearrange the array structure by modifying 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;
    }
}

Upvotes: 1

Related Questions