如何在PHP中将数据存储到多维数组中?

时间:2023-01-26 15:55:59

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 个解决方案

#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...)

还要尽量避免使用字符串(我猜学生姓名)作为键 - 使用学生ID会更好(例如,如果名称被更改,则为图像...)

#2


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

#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...)

还要尽量避免使用字符串(我猜学生姓名)作为键 - 使用学生ID会更好(例如,如果名称被更改,则为图像...)

#2


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