剪切数组以创建从0到5然后从6到11等表格值

问题描述 投票:0回答:2

这很难解释,但这是我的要求:今天我有一个用PHP生成4个数组的表:

@for ($counter_functions = 0; $counter_functions < count($my_array['functions']); $counter_functions++)
	<?php 
		$array_1 = $my_array['functions'][$counter_functions]['statistic']['first_data']['hours'];
		$array_2 = $my_array['functions'][$counter_functions]['statistic']['second_data']['hours'];
		$array_3 = $my_array['functions'][$counter_functions]['statistic']['third_data']['hours'];
		$array_4 = $my_array['functions'][$counter_functions]['statistic']['fourth_data']['hours'];
	?>
	<table>
		<tr>
			<th></th>
			<?php 
				foreach ($array_1 as $key => $value) {
					echo '<th>'.$key.'</th>';
				}
			?>
		</tr>
		<tr>
			<th>Number of connections</th>
			<?php 
				foreach ($array_1 as $key => $value) {
					echo '<th>'.$value.'</th>';
				}
			?>
		</tr>
		<tr>
			<th>Number of successes</th>
			<?php 
				foreach ($array_2 as $key => $value) {
					echo '<th>'.$value.'</th>';
				}
			?>
		</tr>
		<tr>
			<th>Number of errors</th>
			<?php 
				foreach ($array_3 as $key => $value) {
					echo '<th>'.$value.'</th>';
				}
			?>
		</tr>
		<tr>
			<th>Time of response</th>
			<?php 
				// Moyenne des temps de réponse puis arrondir à 4 chiffres
				foreach ($array_4 as $key => $value) {
					echo '<th>'. round(array_sum($value) / count($value), 4) .'</th>';
				}
			?>
		</tr>
	</table>
@endfor

它工作得很好但我想要的是创建具有$ array_1,$ array_2,$ array_3和$ array_4的值的表,从0到5然后是6到11等......就像这样:

<table>
  <tr>
    <th>Key 0</th>
    <th>Key 1</th>
    <th>Key 2</th>
    <th>Key 3</th>
    <th>Key 4</th>
    <th>Key 5</th>
  </tr>
  <tr>
    <th>Value 0</th>
    <th>Value 1</th>
    <th>Value 2</th>
    <th>Value 3</th>
    <th>Value 4</th>
    <th>Value 5</th>
  </tr>
  <tr>
    <th>Value 0</th>
    <th>Value 1</th>
    <th>Value 2</th>
    <th>Value 3</th>
    <th>Value 4</th>
    <th>Value 5</th>
  </tr>
  <tr>
    <th>Value 0</th>
    <th>Value 1</th>
    <th>Value 2</th>
    <th>Value 3</th>
    <th>Value 4</th>
    <th>Value 5</th>
  </tr>
  <tr>
    <th>Value 0</th>
    <th>Value 1</th>
    <th>Value 2</th>
    <th>Value 3</th>
    <th>Value 4</th>
    <th>Value 5</th>
  </tr>
</table>

And then 

<table>
  <tr>
    <th>Key 6</th>
    <th>Key 7</th>
    <th>Key 8</th>
    <th>Key 9</th>
    <th>Key 10</th>
    <th>Key 11</th>
  </tr>
  <tr>
    <th>Value 6</th>
    <th>Value 7</th>
    <th>Value 8</th>
    <th>Value 9</th>
    <th>Value 10</th>
    <th>Value 11</th>
  </tr>
  <tr>
    <th>Value 6</th>
    <th>Value 7</th>
    <th>Value 8</th>
    <th>Value 9</th>
    <th>Value 10</th>
    <th>Value 11</th>
  </tr>
  <tr>
    <th>Value 6</th>
    <th>Value 7</th>
    <th>Value 8</th>
    <th>Value 9</th>
    <th>Value 10</th>
    <th>Value 11</th>
  </tr>
  <tr>
    <th>Value 6</th>
    <th>Value 7</th>
    <th>Value 8</th>
    <th>Value 9</th>
    <th>Value 10</th>
    <th>Value 11</th>
  </tr>
</table>

一个数组可以有5个值,因为它可以有28个值或15个值,取决于它具有的数据。

我至少尝试了一下:我试图制作一个数组的php计数函数来确定它的值的数量然后我做了php ceil函数就可以了

ceil(count($array_1));

使用array_slice函数创建一个循环,使array_slice为$ array1,0,5,然后是6,11。

但我真的很困惑,我迷失在所有这些代码中,有没有比我现在做的更好的方法?

php arrays laravel html-table
2个回答
2
投票

最简单的方法是使用array_chunk将数组划分为所需的数量。

例如:

$Chunked = array_chunk($my_array['functions'], 5);

foreach ($Chunked as $Group ) {
    echo '<table>';
    foreach ($Group as $data) {
        echo '<tr>';
        foreach ($data as $k => $v) {
            echo '<th>'.$k.'</th>';
            echo '<td>'.$v.'</td>';
        }
        echo '</tr>';
    }
    echo '</table>';
}

有关更多信息,请查看文档:http://php.net/array_chunk


0
投票

使用2个嵌套的for循环,您可以轻松实现:

// The number of item you want per row
$column_count = 5;

echo '<table>';

// each iteration will build a row
for ($i = 0; $i < count($my_array); $i += $column_count) {
    echo '<tr>';

    // Write the cells
    for ($j = 0; $j < $column_count; $j += 1) {
        // Verify if the calculated index exists
        if (isset($my_array[$i + $j])) {
            echo '<td>' . $my_array[$i + $j]['my_data'] . '</td>';
        } else {
            echo '<td></td>';
        }
    }
    echo '</tr>';
}
echo '</table>';
© www.soinside.com 2019 - 2024. All rights reserved.