dongtan7351 2014-08-22 22:18
浏览 14
已采纳

在同一个表中显示两个数组

$row = $query->fetchAll(PDO::FETCH_ASSOC);
$num_rows = count($row);

for ($i = 0; $i < $num_rows; $i++)
{
    $title = htmlspecialchars($row[$i]['title']);
    $author =htmlspecialchars($row[$i]['author']);
    $school =htmlspecialchars($row[$i]['school']);
    $solution = $row[$i]['solution'];
    $notes = $row[$i]['notes'];

    $ad = array($title, $price, $author, $school, $contact, $content, $date);
    $inlcude = array($solutions, $notes);

    $field = 0;
    echo "<table border='1'>";    
    // foreach($inlcude as $in) This failled miserably 
    foreach ($ad as $post)
    {
        if ($field < 3) //The first three values are placed in the first row

        {
            echo "<td>$post</td>"; 
        }
        if ($field >= 3) 
        {         
            echo "<tr><td>$post</td><td>$in</td></tr>";   
        }
        $field++;
    }
    echo '</table>';
}

I have two arrays and I would like to display them in different columns in my table. $ad displays perfectly fine but I'm having trouble displaying the contents in $inlcude in the second column. I've tried putting another foreach loop to iterate through contents of the second array but that really screws up my table by placing random values in different places on the table. Besides the foreach loop, I don't know of any other way to iterate through the array. Any suggestions would be appreciated.Thanks!

I want the graph to look like this where $p=$post and $i=$in. Moreover, three columns in first row and two columns in every row after that

$p $p $p 

$p $i 

$p $i 
  • 写回答

1条回答 默认 最新

  • douyan2470 2014-08-23 04:10
    关注

    Assuming that your arrays are formatted correctly, you probably want to use array_shift(). Try something like this:

    // Start by copying the $include array, because array_shift() is a destructive
    //   operation and you might want to use $includes again.
    $includes_copy = $include;
    // Start with your leading <tr> cell.
    echo "<tr>";
    // Now loop your ad array.
    foreach ($ad as $post) {
      //The first three values are placed in the first row.
      if ($field < 3) {
        echo "<td>$post</td>"; 
        $field++;
      }
      if ($field == 3) {
        echo "</tr>";  // Closing tags are good form.
      }
      if ($field >= 3) {
        // Using array_shift() will return the first element from the array.
        // The returned element will be removed from the array.
        $in = array_shift($includes_copy);
        // $post is populated from foreach(), $in is populated by array_shift(). 
        echo "<tr><td>$post</td><td>$in</td><td></td></tr>";
        $field += 3;   
      }
    }
    

    Basically, the concept is that foreach($array as $val) is logically equivalent to while($val = array_shift($array)), meaning that you can run two foreach() at the same time. The only difference is that array_shift() is destructive.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历