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.

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

报告相同问题?

悬赏问题

  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题
  • ¥15 使用Java milo连接Kepserver服务端报错?
  • ¥15 用ADS设计一款的射频功率放大器
  • ¥15 怎么求交点连线的理论解?
  • ¥20 软件开发方法学习来了
  • ¥15 微信小程序商城如何实现多商户收款 平台分润抽成