2个不同长度的数组
$status = array(
array("up","down","down","up"),
array("down","up","up","down"));
$room = array("RM101", "RM102","RM103","RM104","RM105", "RM106","RM107","RM108");
我们如何才能有2列,Top标题为$room
数组,行为$status
。
表格应如下所示:
请帮我解决这个问题,我是PHP编程的新手。
请参阅下面我尝试过的编码。
<?PHP
function room(){
$n = 0;
$rooms = array("RM101","RM102","RM103","RM104","RM105","RM106","RM107","RM108");
{
if ($n % 24 == 0) {
echo '<tr><td>'.$rooms[$m].'</td>';
}
else
{
echo '<td>'.$rooms[$m].'</td>';
}
$n++;
}
}
function status(){
$c=0;
$nodes = array('http://x.x.xx.xx/rpFirstPageStatistics.html','http://x.x.xx.xx/rpFirstPageStatistics.html');
$node_count = count($nodes);
$curl_arr = array();
$master = curl_multi_init();
for($i = 0; $i < $node_count; $i++)
{
$Username = "xxx";
$Password = "xxx";
$url =$nodes[$i];
$curl_arr[$i] = curl_init($url);
curl_setopt($curl_arr[$i], CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_arr[$i], CURLOPT_USERPWD, "$Username:$Password");
curl_multi_add_handle($master, $curl_arr[$i]);
}
do {
curl_multi_exec($master,$running);
} while($running > 0);
for($i = 0; $i < $node_count; $i++)
{
$results = curl_multi_getcontent ( $curl_arr[$i] );
preg_match_all('~<\/td><TD width="65">(.*?)<\/td>~s', $results, $status);
foreach ($status as $value => $values){
}
foreach (array_slice($values,2) as $x=>$x_value){
if ($c % 24 == 0) {
echo '<tr><td>'.$x_value.'</td>';
}else {
echo '<td>'.$x_value.'</td>';
}
$c++;
}
}
}
?>
答案 0 :(得分:1)
array functions可以帮助您完成所有这些工作。
您可以使用array_merge(...$status)
将$status
中的子数组合并到一个包含8个项目的数组中。如果您的PHP太旧而无法使用...
运算符,则可以使用call_user_func_array("array_merge", $status)
。
假设$status
数组总共包含与$room
数组相同数量的项目,则可以将它们组合成一个列表,其中房间为键,状态为array_combine
的值。然后你有一个像这样的数组:
["RM101" => "up", "RM102" => "down", "RM103" => "down", ...]
要将其拆分为包含两列的行,请使用array_chunk
并将$preserve_keys
标记设置为true。
[["RM101" => "up", "RM102" => "down"], ["RM103" => "down", ...], ...]
现在数组大致处于您想要的结构中,它只是循环遍历行并输出HTML的情况。在每个块中,我们可以使用array_keys
获取一对房间名称,array_values
(这不是绝对必要的)来获取这对状态,并implode
加入数组一起变成一个字符串。
把它们放在一起你得到
$status = array(
array("up","down","down","up"),
array("down","up","up","down"));
$room = array("RM101", "RM102","RM103","RM104","RM105", "RM106","RM107","RM108");
echo "<table>";
foreach (array_chunk(array_combine($room, array_merge(...$status)), 2, true)
as $block) {
echo "<tr><th>" . implode("</th><th>", array_keys($row)) . "</th></tr>";
echo "<tr><td>" . implode("</td><td>", array_values($row)) . "</td></tr>";
}
echo "</table>";
答案 1 :(得分:-1)
请尝试以下格式:
<强> HTML:强>
<table>
<thead>
<tr>
<th>Product</th>
<th>Width</th>
<th>Material</th>
</tr>
</thead>
<tbody>
<tr>
<th><img /></th>
<td>50mm</td>
<td>Plastic</td>
</tr>
</tbody>
</table>
<强> PHP:强>
foreach ($rows as $row) {
$similar_com .= "<tr>";
$similar_com .= "<td><img style='width: 100px; height: 100px;' src='$row->image_url' alt='$row->model_no' /><br />$row->name</td>";
$similar_com .= "<td>$row->width mm</td>";
...
}