我有一个如下所示的数组,我需要将该数组显示在表中。我已经尝试使用PHP的函数,但它获取行和单元格值,我需要三种类型的值:
注意:我已将下面的array1转换为数组2
数组1:
Array
(
[0] => Array ([id] => 1[rows] => R1[columns] => c1[name] => 1)
[1] => Array([id] => 2[rows] => R1[columns] => c2[name] => 2)
[2] => Array([id] => 3[rows] => R1[columns] => c3[name] => 3)
[3] => Array([id] => 4[rows] => R2[columns] => c1 [name] => 1)
[4] => Array([id] => 5[rows] => R2[columns] => c2[name] => 2)
[5] => Array([id] => 6[rows] => R2[columns] => c3[name] => 3)
)
数组2:
Array
(
[0] => Array([0] => R1[1] => c1[2] => 1)
[1] => Array([0] => R1[1] => c2[2] => 2)
[2] => Array([0] => R1[1] => c3[2] => 3)
[3] => Array([0] => R2[1] => c1[2] => 1)
[4] => Array([0] => R2[1] => c2[2] => 2)
[5] => Array([0] => R2[1] => c3[2] => 3)
)
表:
id | row | column | value
--------------------------
1 | r1 | c1 | 1
2 | r1 | c2 | 2
3 | r1 | c3 | 3
4 | r2 | c1 | 1
5 | r2 | c2 | 2
6 | r2 | c3 | 3
功能:此函数将array2转换为表
function convetTable($array) {
$rows = array();
$rows[0] = array();
foreach ($array as $column) {
if (!in_array($column[1],$rows[0])) {
$rows[0][] = $column[1];
}
}
$count = count($rows[0]);
foreach ($array as $row) {
$rowTtl = $row[0];
$rowQty = $row[1];
$rowVal = $row[2];
if (!isset($rows[$rowTtl])) $rows[$rowTtl] = array_pad(array(),$count,0);
$rows[$rowTtl][array_search($rowQty,$rows[0])] = $rowVal;
}
return $rows;
}
$table = convetTable($array2);
HTML :
{foreach from=$table item=key key=foo}
<tr class="prototype" id="first_row">
<td><a href="javascript:void(0);" class="remove">Remove Row</a>
<td><input type="text" name="rows[]" value="{$foo}" class="number" /></td>
{foreach from=$key item=cell}
<td align="center"><input type="text" name="cols[]" value="{$cell}" class="id number" /></td>
{/foreach}
</tr>
{/foreach}
预期OutPut
0 C1 C2 C3
R1 1 2 3
R2 1 2 3
PS:这里c1,c2,c3,1,2,3是$ cell value。
我尝试显示$ row,$ col,$ name,但在这里我只获得$ row和$ cell。
我需要相应行和列的另一个值名称。
答案 0 :(得分:-1)
试试这个..
$count = count($array); //this will give you the count of elements of your array...
echo "<table border="1"><tr><th>ID</th><th>ROWS</th><th>COLUMNS</th><th>NAMES</th></tr>";
for($i=1;$i<=$count;$i++) //loop through the array..
{
echo "<tr><td>$i</td><td>".$array[$i][0]."</td><td>".$array[$i][1]."</td><td>".$array[$i][2]."</td></tr>";
}
echo "</table>";
请注意此代码是为了向您展示如何简化您的Massiave代码......您可能需要编辑此代码才能获得所需的输出...