如何从2个数组生成PHP中的表列和行

http://img4.mukewang.com/60950ccf0001333a06630562.jpg

对所有可能为我的问题感到生气或生气的人表示歉意,我们每天都在学习。我希望能够从表中还显示的这两个不同的数组中生成一张表格,使它看起来像这张图片中的图片。


我可以使用foreach循环生成一个普通表,但是在这种情况下,它变得令人困惑,并且我无法尝试任何有形的尝试。因此,任何帮助将不胜感激。


原始数组示例


$data1 = array(

     '0' => array('time'=> '1.00pm', 'reading' =>'271', 'machine'=>'machine1'),

     '1' => array('time'=> '2.00pm', 'reading' =>'261', 'machine'=>'machine1'),

     '2' => array('time'=> '3.00pm', 'reading' =>'172', 'machine'=>'machine1'),

     '3' => array('time'=> '4.00pm', 'reading' =>'300', 'machine'=>'machine1'),

     '4' => array('time'=> '5.00pm', 'reading' =>'142', 'machine'=>'machine1')

    );

$data2 = array(

     '0' => array('time'=> '1.00pm', 'reading' =>'500', 'machine'=>'machine2'),

     '1' => array('time'=> '2.00pm', 'reading' =>'432', 'machine'=>'machine2'),

     '2' => array('time'=> '3.00pm', 'reading' =>'109', 'machine'=>'machine2'),

     '3' => array('time'=> '4.00pm', 'reading' =>'44', 'machine'=>'machine2'),

     '4' => array('time'=> '5.00pm', 'reading' =>'18', 'machine'=>'machine2')

    );

因此,任何帮助将不胜感激


繁星淼淼
浏览 132回答 2
2回答

猛跑小猪

您可以尝试此以获得所需的结果$header = ['machine','1.00pm','2.00pm','3.00pm','4.00pm','5.00pm'];$html = "<table border='1'><tr>";for($i=0; $i<count($header); $i++){&nbsp; $html.= "<th>".$header[$i]."</th>";}$html .= "</tr>";$res = [&nbsp; &nbsp; array_merge(&nbsp; &nbsp; &nbsp; &nbsp; array_unique(array_column($data1,'machine')),&nbsp; &nbsp; &nbsp; &nbsp; array_column($data1, 'reading','time')&nbsp; &nbsp; ),&nbsp; &nbsp; array_merge(&nbsp; &nbsp; &nbsp; &nbsp; array_unique(array_column($data2,'machine')),&nbsp; &nbsp; &nbsp; &nbsp; array_column($data2, 'reading','time')&nbsp; &nbsp; )];foreach($res as $key => $value){&nbsp;$html&nbsp; .= "<tr>";&nbsp;foreach($header as $ikey => $ivalue){&nbsp; &nbsp; $index = ($ivalue == 'machine') ? 0 : $ivalue;&nbsp; &nbsp; $html .= "<td>".$res[$key][$index]."</td>";&nbsp;}&nbsp;$html&nbsp; .= "</tr>";}$html .= "</table>";模式二。$header = ['time','machine1','machine2'];$html = "<table border='1'><tr>";for($i=0; $i<count($header); $i++){&nbsp; $html.= "<th>".$header[$i]."</th>";}$res = [&nbsp; array_merge(&nbsp; &nbsp; array_column($data1, 'reading','time')&nbsp;),&nbsp;array_merge(&nbsp; &nbsp; array_column($data2, 'reading','time')&nbsp;)];for($i=0;$i<count($res)-1;$i++){&nbsp; &nbsp; foreach($res[$i] as $key => $value){&nbsp; &nbsp; &nbsp;$html .= '<tr>';&nbsp; &nbsp; &nbsp;$html .= '<td>'.$key.'</td>';&nbsp; &nbsp; &nbsp;$html .= '<td>'.$res[$i][$key].'</td>';&nbsp; &nbsp; &nbsp;$html .= '<td>'.$res[$i+1][$key].'</td>';&nbsp; &nbsp; $html .= '</tr>';&nbsp;}&nbsp; &nbsp;}&nbsp;echo $html;

慕勒3428872

好吧,这个答案包含了所有表格<?php$data1 = array(&nbsp;'0' => array('time'=> '1.00pm', 'reading' =>'271', 'machine'=>'machine1'),&nbsp;'1' => array('time'=> '2.00pm', 'reading' =>'261', 'machine'=>'machine1'),&nbsp;'2' => array('time'=> '3.00pm', 'reading' =>'172', 'machine'=>'machine1'),&nbsp;'3' => array('time'=> '4.00pm', 'reading' =>'300', 'machine'=>'machine1'),&nbsp;'4' => array('time'=> '5.00pm', 'reading' =>'142', 'machine'=>'machine1'));$data2 = array(&nbsp;'0' => array('time'=> '1.00pm', 'reading' =>'500', 'machine'=>'machine2'),&nbsp;'1' => array('time'=> '2.00pm', 'reading' =>'432', 'machine'=>'machine2'),&nbsp;'2' => array('time'=> '3.00pm', 'reading' =>'109', 'machine'=>'machine2'),&nbsp;'3' => array('time'=> '4.00pm', 'reading' =>'44', 'machine'=>'machine2'),&nbsp;'4' => array('time'=> '5.00pm', 'reading' =>'18', 'machine'=>'machine2'));echo $data1[0]['machine'];echo '<table>';echo '<tr>';echo "<th>Time</th>";echo "<th>reading</th>";echo '</tr>';foreach ($data1 as $key => $value) {&nbsp; echo "<tr>";&nbsp; echo "<td>" . $value['time'] . "</td>";&nbsp; echo "<td>" . $value['reading'] . "</td>";&nbsp; echo "</tr>";}echo '</table>';echo '<br>';&nbsp; &nbsp; echo $data2[0]['machine'];&nbsp; &nbsp; echo '<table>';&nbsp; &nbsp; echo '<tr>';&nbsp; &nbsp; echo "<th>Time</th>";&nbsp; &nbsp; echo "<th>reading</th>";&nbsp; &nbsp; echo '</tr>';&nbsp; &nbsp; foreach ($data2 as $key => $value) {&nbsp; &nbsp; &nbsp; echo "<tr>";&nbsp; &nbsp; &nbsp; echo "<td>" . $value['time'] . "</td>";&nbsp; &nbsp; &nbsp; echo "<td>" . $value['reading'] . "</td>";&nbsp; &nbsp; &nbsp; echo "</tr>";&nbsp; &nbsp; }&nbsp; &nbsp; echo '</table>';&nbsp; &nbsp; echo '<br>';&nbsp; &nbsp; echo '<br>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo '<table>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<tr>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<th>machin</th>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach ($data1 as $key => $value) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<th>" . $value['time'] . "</th>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "</tr>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<tr>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<td>machine1</td>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach ($data1 as $key => $value) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<td>" . $value['reading'] . "</td>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "</tr>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<tr>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<td>machine2</td>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach ($data1 as $key => $value) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<td>" . $data2[$key]['reading'] . "</td>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "</tr>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo '</table>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo '<br>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo '<br>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo '<table>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo '<tr>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<th>Time</th>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<th>machine1</th>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<th>machine2</th>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo '</tr>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach ($data2 as $key => $value) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<tr>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<td>" . $value['time'] . "</td>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<td>" . $data1[$key]['reading'] . "</td>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<td>" . $value['reading'] . "</td>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "</tr>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo '</table>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo '<br>';&nbsp;?>
打开App,查看更多内容
随时随地看视频慕课网APP