【问题标题】:Display two arrays in two columns of a table在表格的两列中显示两个数组
【发布时间】:2016-06-06 14:16:02
【问题描述】:

我有两个按名称排列的数组

$names = array(1A,1B,1C,2A,2B,2C,3A,3B,3C);
    $user_names = array(Jen, Smith, Nick, Rose, Jason, Ralph, Bruce, Linda, Kate);

<table class="stak">
    <?php
    $i = 1;
    foreach ($names as $name) {
        ?>
        <tr>
            <td> <?php echo $i ?> </td>
            <td> <?php echo $name ?> </td>
            <td> <?php echo $user_name ?> </td>
        <?php
        $i++;?>
        </tr><?php 
    }
    $j = 1;
    foreach ($user_names as $user_name) {
        ?>
        <tr>
            <td> <?php echo $j ?> </td>
            <td> <?php echo $user_name ?> </td> 
        <?php
        $j++;?>
        </tr><?php
    }
    ?>
</table>

我想在一个表格中显示它们,其中包含 3 列 Sl NO、名称、PHP 中的用户名 请帮忙。

【问题讨论】:

  • 你的数组也有语法错误。
  • 尝试了 foreach 循环,但整个事情都搞砸了。
  • 你能告诉我们你尝试了什么,以及你想要的输出吗?这个问题还不清楚……
  • 我投票结束这个问题,因为您需要澄清您的具体问题或添加其他详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。请参阅How to Ask 页面以获得澄清此问题的帮助。

标签: php arrays html html-table


【解决方案1】:

解决方案:

$names = array("1A","1B","1C","2A","2B","2C","3A","3B","3C");
    $user_names = array("Jen", "Smith", "Nick", "Rose", "Jason", "Ralph", "Bruce","Linda", "Kate");

    $combine = array_combine($names, $user_names);



    $html = "<table>";
    $html .= "<tr><td>Sl.No</td><td>Name</td><td>Username</td></tr>";
    $i = 1;
    foreach ($combine as $names =>  $user_names):

        $html .= "<tr>";
        $html .= "<td>".$i."</td>";
        $html .= "<td>".$names."</td>";
        $html .= "<td>".$user_names."</td>";
        $html .= "</tr>";

        $i++;
    endforeach;
    $html .= "</table>";

    echo $html;

输出:

Sl.No   Name    Username
1   1A  Jen
2   1B  Smith
3   1C  Nick
4   2A  Rose
5   2B  Jason
6   2C  Ralph
7   3A  Bruce
8   3B  Linda
9   3C  Kate

第二种情况(如果有超过 2 个数组):

$names = array("1A","1B","1C","2A","2B","2C","3A","3B","3C");
$user_names = array("Jen", "Smith", "Nick", "Rose", "Jason", "Ralph", "Bruce","Linda", "Kate");
$dob = array("12","13","14","15","16","17","18","19","20");
$height = array("6","7","8","5","4","7","5","9","5");


$html = "<table>";
$html .= "<tr><td>Sl.No</td><td>Name</td><td>Username</td><td>dob</td><td>height</td></tr>";

foreach ($names as $id => $key):

        $html .= "<tr>";
        $html .= "<td>".($id+1)."</td>";
        $html .= "<td>".$key."</td>";
        $html .= "<td>".$user_names[$id]."</td>";
        $html .= "<td>".$dob[$id]."</td>";
        $html .= "<td>".$height[$id]."</td>";
        $html .= "</tr>";

endforeach;

$html .= "</table>";
echo $html;

【讨论】:

  • 感谢@DeepakMankotia,.. 工作,.
  • 这样可以组合多少个数组?因为我还有 2 个数组要显示在同一个表中的列中。
  • 这种情况下需要使用for循环。
  • 你能告诉我怎么做吗?
  • 请在此处告诉我您的要求,以便我可以将代码发送给您
【解决方案2】:

这是一种在数组中显示数据的简单快速的方法

<?php

// DATAS
$names = array('1A','1B','1C','2A','2B','2C','3A','3B','3C');
$user_names = array('Jen','Smith','Nick','Rose','Jason','Ralph','Bruce','Linda','Kate');

// HTML
$table = '<table><thead><tr>';
$table .= '<th>Name</th><th>User name</th>';
$table .= '</tr></thead><tbody>';

for ($i=0; $i < count($names) ; $i++) {
  $table .= '<tr>';
  $table .= '<td>'.$names[$i].'</td>';
  $table .= '<td>'.$user_names[$i].'</td>';
  $table .= '</tr>';
}

$table .= '</tbody>';

echo $table;

这是html结果

<table>
  <thead>
    <tr>
     <th>Name</th>
     <th>User name</th>
    </tr>
  </thead>
  <tbody>
   <tr>
    <td>1A</td>
    <td>Jen</td>
   </tr>
   <tr>
     <td>1B</td>
     <td>Smith</td>
   </tr>
   ...
 </tbody>

【讨论】:

  • 非常感谢@Kevin,。
【解决方案3】:

试试这个。有用。我测试过。

$names = array('1A','1B','1C','2A','2B','2C','3A','3B','3C');
$user_names = array('Jen', 'Smith', 'Nick', 'Rose', 'Jason', 'Ralph', 'Bruce', 'Linda', 'Kate');

$size = count($names);

$output = "<table>";
$output .= "<tr><td>Sl.No</td><td>Name</td><td>Username</td></tr>";

for($i=0; $i<$size; $i++) {
    $output .= "<tr>";
    $output .= "<td>".($i+1)."</td>";
    $output .= "<td>".$names[$i]."</td>";
    $output .= "<td>".$user_names[$i]."</td>";
    $output .= "</tr>";
}

$output .= "</table>";

echo $output;

【讨论】:

  • 正是我所写的...使用for 循环同时遍历两个数组。好答案
猜你喜欢
  • 2017-03-31
  • 2019-11-09
  • 1970-01-01
  • 2018-06-27
  • 1970-01-01
  • 2017-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多