0

I know this is a dumb question, but is there a way to produce a code that will display the way I wanted it do like below?:

 array()
    0=>
       'label'=> string 'John'
       'start'=>
          array()
             0=> string '2015-01-17'
             1=> string '2015-01-16'
       'end' =>
          array()
             0=> string '2015-01-18'
             1=> string '2015-01-16'
       'class'=>
          array()
             0=> string 'annual'
             1=> string 'sick'
    1=>
       'label'=> string 'Peter'
       'start'=>
          array()
             0=> string '2015-01-02'
       'end' =>
          array()
             0=> string '2015-01-05'
       'class'=>
          array()
             0=> string 'annual'
    2=>
       'label'=> string 'Mark'
       'start'=>
          array()
             0=> string '2015-01-08'
             1=> string '2015-01-09'
       'end' =>
          array()
             0=> string '2015-01-08'
             1=> string '2015-01-09'
       'class'=>
          array()
             0=> string 'sick'
             1=> string 'annual'

i have been studying an array lately and i saw a code and modify it to look like this:

     $data[] = array(
      'label' => $row["name"] ,
      'start' => array($row["start_date"]), 
      'end'   => array($row["end_date"]),
      'class' => array($row["type"]));

and the code displays the following although it's not what I intended it to do.

 array (size=6)
 0 => 
  array (size=4)
    'label' => string 'John' (length=4)
    'start' => 
      array (size=1)
        0 => string '2015-01-17' (length=10)
   'end' => 
      array (size=1)
        0 => string '2015-01-18' (length=10)
   'class' => 
       array (size=1)
        0=> string 'annual' (length=6)
 1 => 
  array (size=4)
    'label' => string 'John' (length=4)
    'start' => 
      array (size=1)
        0 => string '2015-01-16' (length=10)
    'end' => 
      array (size=1)
        0 =>string '2015-01-16' (length=10)
    'class' => 
       array (size=1)
        0=>string 'sick' (length=4)
 2 => 
  array (size=4)
    'label' => string 'Peter' (length=5)
    'start' => 
      array (size=1)
        0 => string '2015-01-02' (length=10)
     'end' => 
       array (size=1)
         0 => string '2015-01-05' (length=10)
     'class' => 
       array (size=1)
        0=>string 'sick' (length=4)
3 => 
  array (size=4)
    'label' => string 'Mark' (length=19)
    'start' => 
      array (size=1)
       0 => string '2015-01-08' (length=10)
    'end' => 
     array (size=1)
       0 => string '2015-01-08' (length=10)
   'class' => 
       array (size=1)
        0=>string 'sick' (length=4)
4 => 
  array (size=4)
   'label' => string 'Mark' (length=19)
   'start' => 
     array (size=1)
       0 => string '2015-01-09' (length=10)
   'end' => 
     array (size=1)
      0 => string '2015-01-09' (length=10)
   'class' => 
        array (size=1)
        0=>string 'annual' (length=6)
2
  • i think you already need to play a little bit of how you manage assigning,, i can see it in your code that you are already close on what you want Commented May 6, 2015 at 8:14
  • @OliSoproniB. thank you oli, I have been studying it for weeks but still couldn't sort it out yet.. ^_^ Commented May 6, 2015 at 8:23

2 Answers 2

1

You can try (providing random data)

$row['name'] = 'John';
$row['start_date_1'] = '2015-01-01';
$row['start_date_2'] = '2015-01-02';

$row['end_date_1'] = '2015-08-11';
$row['end_date_2'] = '2015-08-22';

$row['class_type_1'] = 'sick';
$row['class_type_2'] = 'annual';

$data[] = array(
  'label' => $row["name"] ,
  'start' => array($row["start_date_1"], $row["start_date_2"]),
  'end'   => array($row["end_date_1"], $row["end_date_2"]),
  'class' => array($row["class_type_1"], $row["class_type_2"]));

Also try to avoid strings (I suppose student names) as keys - using student ID would be better (eg, image if the name is changed...)

Sign up to request clarification or add additional context in comments.

8 Comments

thanks for the response. i was wondering what if my start value is more than two?... as i've seen in your example code you manually type array($row["start_date_1"], $row["start_date_2"]),..
@LBMG What do you mean by start value is more than two? Since I don't know what the two dates eg. '2015-01-17' and '2015-01-16' mean I just assumed semi-random keys.
the result is like this array(size=6) 0=> array(size=4) 'label'=> string 'John'(length=4) 'start'=> array(size=1) 0=> string '2015-01-17'(length=10) 'end' => array(size=1) 0=> string '2015-01-18'(length=10) 'class'=> array(size=1) 0=> string 'annual'(length=6) 1=> array(size=4) 'label'=> string 'John'(length=4) 'start'=> array(size=1) 0=> string '2015-01-16'(length=10) 'end' => array(size=1) 0=> string '2015-01-16'(length=10) 'class'=> array(size=1) 0=> string 'sick'(length=4)
Are you sure the values are legit?
yes it is... all values was fetched from the database.. my db is as below reference_no | name | start_date | end_date | type | 1 | John |2015-01-17|2015-01-18| annual | 2 |John |2015-01-16|2015-01-16|sick | 3 |Peter |2015-01-02|2015-01-05|annual |
|
1
$data[] = array(
      'label' => $row["name"] ,
      'start' => $row["start_date"], 
      'end'   => $row["end_date"],
      'class' => $row["type"],);

1 Comment

thank you for your response, I have already tried that code but its still not what i intended it do display... and the data on this code wasn't sorted.

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.