PHP中的动态数组

发布于 2024-12-18 00:32:44 字数 1240 浏览 7 评论 0原文

我有下一个:

$students=array('student1','student2','student3','student4');
$date=array('date1','date2','date3');

$students 代表文件, $date 代表列。 对于每个 $student 和 $date 都有一个不同的值。

例如:

  $students['student1']$dates['date1']='value11';
  $students['student1']$dates['date2']='value12';
  $students['student1']$dates['date3']='value13';
  $students['student2']$dates['date1']='value21';

.... …… 等等。

           date1   date2     date3 
   student1   value11  value12  value13
   student2   value21  value22  value23
   student3   value31  value32  value33

我想填写表格中的值。 我想动态地将这些值加载到网格中。 $students$dates 的计数动态变化。
这些值填写在我需要插入数据库的表中。 我需要将值更新到数据库中。

我在数据库中的表有以下字段:

id_tabla  estudiante   date    values

1         estudiante1  date1  value11
2         estudiante1  date2  value12
3         estudiante1  date3  value13
4         estudiante2  date1  value21
5         estudiante2  date2  value22
6         estudiante2  date3  value23

你能帮助我吗?谢谢。

$students 是通过数据库中的查询生成的。 $dates 由 php 中的一系列日期生成。

这个想法是制作一个带有文本输入 $students x $dates 的表单,其中应包含数据。通过这种形式,我应该获得值数据。

I have the next:

$students=array('student1','student2','student3','student4');
$date=array('date1','date2','date3');

$students represents the files and $date represents the columns.
For each $student and $date there is a value different.

For example:

  $students['student1']$dates['date1']='value11';
  $students['student1']$dates['date2']='value12';
  $students['student1']$dates['date3']='value13';
  $students['student2']$dates['date1']='value21';

....
.....
etc.

           date1   date2     date3 
   student1   value11  value12  value13
   student2   value21  value22  value23
   student3   value31  value32  value33

I want fill the values in a table.
I want to load these values in a grid dynamically.
The count for $students and $dates change dynamically.
These values filled in a table I need to insert into database.
I need to update the values into database.

My table in database has the follow fields:

id_tabla  estudiante   date    values

1         estudiante1  date1  value11
2         estudiante1  date2  value12
3         estudiante1  date3  value13
4         estudiante2  date1  value21
5         estudiante2  date2  value22
6         estudiante2  date3  value23

can you help me? Thanks.

The $students is genertated by a query in database.
$dates is generated by a range of the dates in php.

The idea is to do a form with text input $students x $dates where the datas should be included. With this form I should get the values datas.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

幽蝶幻影 2024-12-25 00:32:44

您有很多要求,并且在不了解上下文的情况下,选择正确的数据结构并非易事。从你的例子来看,我猜你有一个包含学生的数组和一个包含日期的数组。数组的大小可以更改,但您始终需要将日期数组附加到每个学生,对吧?

因此,根据我的假设,我认为您不需要将两个数组重新排列为一个。您可以使用给定的结构完成您的任务。

在表中输出每个学生的日期:

<table>
  <?php foreach($students as $student) { ?>
  <tr>
    <td><?php echo $student; ?></td>
    <?php foreach($dates as $date) { ?>
      <td><?php echo $date; ?></td>
    <?php } ?>
  </tr>
  <?php } ?>
</table>

插入数据库:

<?php
$insert_dates = "'" . implode("','", $dates) . "'";
foreach($students as $student) {
  $qry = "INSERT INTO `students` (`name`, `date1`, `date2`, `date3`)
                 VALUES ('" . $student . "', " . $insert_dates . ")";
  //execute query
}

更新:

<?php
  $qry = "UPDATE `students` 
          SET date1='".$dates[0]."', date2='".$dates[1]."', date2='".$dates[2]."' 
          WHERE name IN ('" . implode("','", $students) . "')";
  //execute query

但是这种方法在动态变化方面存在一些明显的缺陷。因此,如果您要使用数据库,则应该使用与表的列名相对应的键构建关联数组:

$students=array();
$students[] = array('name' => 'student1');
$students[] = array('name' => 'student2');
$students[] = array('name' => 'student3');
$students[] = array('name' => 'student4');

$dates=array();
$dates[] = array('date1'=>'15/04/20', 'date2'=>'20/05/15', 'date3'=>'18/11/10');

动态更新查询将如下所示:

  $set_data = '';
  foreach($dates as $key => $value) {
    $set_data .= $key . "='".$value."',";
  }
  $set_data = substr($set_data, 0, -1); //remove last comma
  $qry = "UPDATE `students` 
          SET " . $set_data . "  
          WHERE name IN ('" . implode("','", $students) . "')";
  //execute query

通过使用数据结构,您可以实现更多功能(例如,单独应用和更改每个学生的日期,选择学生姓名作为关联数组的键,以便您可以按姓名访问每个学生等) 。因此,首先您应该弄清楚您需要数据的用途,然后据此选择合适的结构。我想您已经明白了,希望对您有所帮助!


//更新

根据您更新的问题,我认为这可能是结构的一个好方法(尽管我仍然不知道数据来自哪里以及如何/是否可以动态填充该数组/programatically):

$students = array();
$students[] = array(
  'name' => 'student1',
  'dates' => array(
     'date1' => 'code123kil',
     'date2' => 'dadfdre145',
  )
);
$students[] = array(
  'name' => 'student2',
  'dates' => array(
     'date1' => 'daytetyeyy',
     'date2' => 'dafdfe335',
     'date3' => 'code123kil'
  )
);
//and so on

现在,让我们将其输出到网格中

<table>
 <?php foreach($students as $student) { ?>
   <tr>
     <td>Name: <?php echo $student['name']; ?></td>
     <td>Dates: <?php echo implode(', ', $student['dates']); ?></td>
   </tr>
 <?php } ?>
</table>

插入表格

<?php
foreach($students as $student) {
  foreach($student['dates'] as $key => $value) {
    $qry = "INSERT INTO `your_table` (`estudiante`, `date`, `values`)
            VALUES ('".$student['name']."','".$key."','".$value."')";
  //execute query
  }
}

更新表格中给定学生的日期值更新

<?php
  //find the given student in the array
  $student_to_update = 'student2';
  $student_info = null;
  foreach($sudents as $student) {
   if($student['name'] == $student_to_update) {
    $student_info = $student;
    break;
   }
  }

  //update the student in database:
  if(!is_null($student_info))
    foreach($student_info['dates'] as $key => $value) {
      $qry = "UPDATE `your_table`
              SET `values`='".$value."'
              WHERE `estudiante` = '".$student_info['name']."' 
                AND `date`='".$key."'";
      //execute query
    }
  }

​表格中每个学生的日期值

<?php
  foreach($students as $student) {
    foreach($student['dates'] as $key => $value) {
      $qry = "UPDATE `your_table`
              SET `values`='".$value."'
              WHERE `estudiante` = '".$student['name']."' 
                AND `date`='".$key."'";
      //execute query
    }    
  }

You have quite a lot of requirements and without knowing the context it's not trivial to choose the right data structure. From your example I guess that you have an array with students and an array with dates. The size of the arrays can change, but you always need to attach the dates-array to each student, right?

So based on my assumption, I think you don't need to rearrange the two arrays into one. You can do your enlisted tasks with the given structure.

Outputting each student with dates in a table:

<table>
  <?php foreach($students as $student) { ?>
  <tr>
    <td><?php echo $student; ?></td>
    <?php foreach($dates as $date) { ?>
      <td><?php echo $date; ?></td>
    <?php } ?>
  </tr>
  <?php } ?>
</table>

Inserting in a database:

<?php
$insert_dates = "'" . implode("','", $dates) . "'";
foreach($students as $student) {
  $qry = "INSERT INTO `students` (`name`, `date1`, `date2`, `date3`)
                 VALUES ('" . $student . "', " . $insert_dates . ")";
  //execute query
}

Updating:

<?php
  $qry = "UPDATE `students` 
          SET date1='".$dates[0]."', date2='".$dates[1]."', date2='".$dates[2]."' 
          WHERE name IN ('" . implode("','", $students) . "')";
  //execute query

BUT this approach has some obvious flaws concerning the dynamic changes. So if you are going to work with databases, you should build associative arrays with the keys corresponding to the column names of the table:

$students=array();
$students[] = array('name' => 'student1');
$students[] = array('name' => 'student2');
$students[] = array('name' => 'student3');
$students[] = array('name' => 'student4');

$dates=array();
$dates[] = array('date1'=>'15/04/20', 'date2'=>'20/05/15', 'date3'=>'18/11/10');

The dynamic update query would then look like so:

  $set_data = '';
  foreach($dates as $key => $value) {
    $set_data .= $key . "='".$value."',";
  }
  $set_data = substr($set_data, 0, -1); //remove last comma
  $qry = "UPDATE `students` 
          SET " . $set_data . "  
          WHERE name IN ('" . implode("','", $students) . "')";
  //execute query

There's a whole lot more you can achieve by playing with data structures (e.g., applying and changing the dates for each student individually, choosing the student-name as the key of the associative array so you can access each student by name, etc.). So at first you should figure out what you need the data for and then choose an appropriate structure based on that. I think you get the idea and I hope it helps!


//UPDATE

Based on your updated question, I think this could be a good approach for the structure (although I still don't know where the data comes from and how/if you can populate that array dynamically/programatically):

$students = array();
$students[] = array(
  'name' => 'student1',
  'dates' => array(
     'date1' => 'code123kil',
     'date2' => 'dadfdre145',
  )
);
$students[] = array(
  'name' => 'student2',
  'dates' => array(
     'date1' => 'daytetyeyy',
     'date2' => 'dafdfe335',
     'date3' => 'code123kil'
  )
);
//and so on

So now, let's output it into a grid:

<table>
 <?php foreach($students as $student) { ?>
   <tr>
     <td>Name: <?php echo $student['name']; ?></td>
     <td>Dates: <?php echo implode(', ', $student['dates']); ?></td>
   </tr>
 <?php } ?>
</table>

Insert into a table:

<?php
foreach($students as $student) {
  foreach($student['dates'] as $key => $value) {
    $qry = "INSERT INTO `your_table` (`estudiante`, `date`, `values`)
            VALUES ('".$student['name']."','".$key."','".$value."')";
  //execute query
  }
}

Update the date-values in a table for a given student

<?php
  //find the given student in the array
  $student_to_update = 'student2';
  $student_info = null;
  foreach($sudents as $student) {
   if($student['name'] == $student_to_update) {
    $student_info = $student;
    break;
   }
  }

  //update the student in database:
  if(!is_null($student_info))
    foreach($student_info['dates'] as $key => $value) {
      $qry = "UPDATE `your_table`
              SET `values`='".$value."'
              WHERE `estudiante` = '".$student_info['name']."' 
                AND `date`='".$key."'";
      //execute query
    }
  }

Update the date-values in a table for each student

<?php
  foreach($students as $student) {
    foreach($student['dates'] as $key => $value) {
      $qry = "UPDATE `your_table`
              SET `values`='".$value."'
              WHERE `estudiante` = '".$student['name']."' 
                AND `date`='".$key."'";
      //execute query
    }    
  }
抠脚大汉 2024-12-25 00:32:44

这有帮助吗:

$students=array('student1','student2','student3','student4');
    $date=array('date1','date2','date3');

$c = 0;
$countArr = count($students);
$newArr = array();
for($c = 0; $c < $countArr; $c++) {
    $x = 0;
    foreach($date as $val) {
        $newArr[$c]["date".($x+ 1)] = $date[$x];
        $x++;
    }
}
echo "<pre>";
print_r($newArr);

Does this help:

$students=array('student1','student2','student3','student4');
    $date=array('date1','date2','date3');

$c = 0;
$countArr = count($students);
$newArr = array();
for($c = 0; $c < $countArr; $c++) {
    $x = 0;
    foreach($date as $val) {
        $newArr[$c]["date".($x+ 1)] = $date[$x];
        $x++;
    }
}
echo "<pre>";
print_r($newArr);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文