Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
foreach循环php中的For循环_Php_Arrays_Codeigniter - Fatal编程技术网

foreach循环php中的For循环

foreach循环php中的For循环,php,arrays,codeigniter,Php,Arrays,Codeigniter,我正在codeigniter中成功地获取学生姓名和主题(生成pdf)。 现在我想获取学生记录以获得如下结果: Student name Drawing Math Computer A 89 66 92 B 65 72 83 C 62 71 86 D 78 73 8

我正在codeigniter中成功地获取学生姓名和主题(生成pdf)。 现在我想获取学生记录以获得如下结果:

Student name   Drawing    Math   Computer
A               89        66     92         
B               65        72     83 
C               62        71     86 
D               78        73     83
E               82        91     82
我得到了学生的名字垂直,现在我希望学生的名字水平像上面的数组,当前数组是

Array
(
    [0] => Array
    (
        [oral] => 30
        [Written] => 20
        [Drawing] => 10
        [Listening] => 70
    )

    [1] => Array
    (
        [oral] => 20
        [Written] => 60
        [Drawing] => 10
        [Listening] => 40
    )

    ...
)
以下是生成视图的代码,其中学生记录垂直显示:

<?php foreach ($studentrecord as $rec) {?>
    <tr>
        <td width="200px"><span><?php echo $rec['StudentName'];?></span></td>
        <td><span>&nbsp;<?php ?></span></td>

        <td><span>&nbsp;</span></td>
        <td><span>&nbsp;</span></td>
        <td><span>&nbsp;</span></td>
    </tr>
<?php  } ?>

现在我想水平提取标记,我该怎么做?

您可以使用这个

<tr>
    <?php foreach ($studentrecord as $rec) { ?>
    <td width="200px"><span><?php echo $rec['StudentName'];?></span></td>
    <?php } ?>
</tr>
<tr>
    <?php foreach ($studentrecord as $rec) { ?>
    <td><span>&nbsp;<?php echo $rec['oral'];?></span></td>
    <?php } ?>
</tr>
<tr>
    <?php foreach ($studentrecord as $rec) { ?>
    <td><span>&nbsp;<?php echo $rec['oral'];?></span></td>
    <?php } ?>
</tr>
<tr>
    <?php foreach ($studentrecord as $rec) { ?>
    <td><span>&nbsp;<?php echo $rec['oral'];?></span></td>
    <?php } ?>
</tr>
<tr>
    <?php foreach ($studentrecord as $rec) { ?>
    <td><span>&nbsp;<?php echo $rec['oral'];?></span></td>
    <?php } ?>
</tr>

请试试这个

        <?php foreach ($studentrecord as $key=> $rec) {?>
        <tr>
            <td width="200px"><span><?php echo $rec[$key]['StudentName'];?></span> 
        </td>
            <td><span>&nbsp;<?php ?></span></td>
            <td><span>&nbsp;</span></td>
            <td><span>&nbsp;</span></td>
            <td><span>&nbsp;</span></td>
        </tr>
        <?php  } ?>

有几种方法。 直觉上我会做一个2倍的循环

<?php
$array = [["oral"=>12,"writen"=>20],["oral"=>13,"writen"=>15],["oral"=>12,"writen"=>18]];
?>
<!DOCTYPE html>
<html>
  <body>
       <table>
      <tr>
        <th>student</th>
        <th>oral</th>
        <th>writen</th>
      </tr>

        <?php
        foreach($array as $key => $value) {
            // $key is index of student
            // $value is his notes.
            echo "<tr>"; 
            echo "<td>".$key."</td>";
            // need to do a second loop for each mark 
            foreach($value as $key2 ) {
                echo "<td>";
                echo $key2;
                echo "<td>";
            }
            echo "</tr>";
        }
        ?>

    </table>
  </body>

</html>

大学生
口头的
书写
输出将是:

您可以试试这个,别忘了验证可能出现的空情况

$studentrecord = Array(
                    0 => Array
                    (
                        'oral' => 30,
                        'Written' => 20,
                        'Drawing' => 10,
                        'Listening' => 70
                    ),
                    1 => Array
                    (
                        'oral' => 20,
                        'Written' => 60,
                        'Drawing' => 10,
                        'Listening' => 40
                    )
);

echo "<table border='1'>";
foreach ($studentrecord as $rec) 
{
    echo "<tr><td>".join($rec,"</td><td>")."</td></tr>";
}
echo "</table>";
$studentrecord=阵列(
0=>数组
(
“口头”=>30,
“书面”=>20,
“图纸”=>10,
“倾听”=>70
),
1=>数组
(
“口头”=>20,
“书面”=>60,
“图纸”=>10,
“倾听”=>40
)
);
回声“;
foreach($studentrecord作为$rec)
{
加入($rec,“”);
}
回声“;

如果您能提出您的问题,那就太好了!另外,不要害怕使用问号(“?”),我想你的意思是说你需要按学生姓名ASC排序。然后在CI中,您可以使用
$this->db->order\u by(“学生姓名”、“asc”)