为什么要清空一个空的td并正常工作。如果没有数据,则将下一行数据推送到列中。请建议我如何解决它。
for($i = 0; $i < count($cheads[$keys[0]]); $i++){
echo "<tr>";
for($k = 0; $k < count($keys); $k++){
echo "<td>";
if(array_key_exists($i, $cheads[$keys[$k]])){
echo $cheads[$keys[$k]][$i]['Data'];
}else{
echo "N/A";
}
echo "</td>";
}
echo "</tr>";
}
Array ( [Last Name] => Array ( [0] => Array ( [Field] => Last Name [Data] => 1 [User] => 5 ) [1] => Array ( [Field] => Last Name [Data] => 1 [User] => 5 ) [2] => Array ( [Field] => Last Name [Data] => 1 [User] => 5 ) [3] => Array ( [Field] => Last Name [Data] => Yo [User] => 5 ) [4] => Array ( [Field] => Last Name [Data] => Test [User] => 5 ) ) [Phone Name] => Array ( [0] => Array ( [Field] => Phone Name [Data] => 2 [User] => 5 ) [1] => Array ( [Field] => Phone Name [Data] => 2 [User] => 5 ) [2] => Array ( [Field] => Phone Name [Data] => 2 [User] => 5 ) [3] => Array ( [Field] => Phone Name [Data] => Yo [User] => 5 ) [4] => Array ( [Field] => Phone Name [Data] => [User] => 5 ) ) [Address Name] => Array ( [0] => Array ( [Field] => Address Name [Data] => 3 [User] => 5 ) [1] => Array ( [Field] => Address Name [Data] => 3 [User] => 5 ) [2] => Array ( [Field] => Address Name [Data] => Yo [User] => 5 ) [3] => Array ( [Field] => Address Name [Data] => [User] => 5 ) ) [Description] => Array ( [0] => Array ( [Field] => Description [Data] => 4 [User] => 5 ) [1] => Array ( [Field] => Description [Data] => 4 [User] => 5 ) [2] => Array ( [Field] => Description [Data] => 4 [User] => 5 ) [3] => Array ( [Field] => Description [Data] => Yo [User] => 5 ) [4] => Array ( [Field] => Description [Data] => [User] => 5 ) ) [Fruit Type] => Array ( [0] => Array ( [Field] => Fruit Type [Data] => Peanuts [User] => 5 ) [1] => Array ( [Field] => Fruit Type [Data] => Apple [User] => 5 ) ) )
答案 0 :(得分:0)
您的PHP实际上没有任何问题。问题源于数组的生成方式。
您根据数字数组键索引识别行,因此当缺少行时,它不会跳过数字索引,而是使用下一行,导致所有内容不同步。
根据您的图表,您期待所有&#39; Yo&#39;记录属于一起,因此您必须保持数组键同步。这意味着跳过电话名称&#39;中的索引2。并且还在水果类型&#39;
中跳过索引0,1和2此
'Address Name' => Array (
'0' => Array ( 'Field' => 'Address Name', 'Data' => 3, 'User' => 5 ),
'1' => Array ( 'Field' => 'Address Name', 'Data' => 3, 'User' => 5 ),
'2' => Array ( 'Field' => 'Address Name', 'Data' => 'Yo', 'User' => 5 ),
'3' => Array ( 'Field' => 'Address Name', 'Data' => null, 'User' => 5 )
),
和
'Fruit Type' => Array (
'0' => Array ( 'Field' => 'Fruit Type', 'Data' => 'Peanuts', 'User' => 5 ),
'1' => Array ( 'Field' => 'Fruit Type', 'Data' => 'Apple', 'User' => 5 )
)
会变成
'Address Name' => Array (
'0' => Array ( 'Field' => 'Address Name', 'Data' => 3, 'User' => 5 ),
'1' => Array ( 'Field' => 'Address Name', 'Data' => 3, 'User' => 5 ),
'3' => Array ( 'Field' => 'Address Name', 'Data' => 'Yo', 'User' => 5 ),
'4' => Array ( 'Field' => 'Address Name', 'Data' => null, 'User' => 5 )
),
'Fruit Type' => Array (
'3' => Array ( 'Field' => 'Fruit Type', 'Data' => 'Peanuts', 'User' => 5 ),
'4' => Array ( 'Field' => 'Fruit Type', 'Data' => 'Apple', 'User' => 5 )
)
因为每条记录中没有其他识别信息,否则无法解决它(每个用户都是5的奇怪)。
一种替代解决方案是更改为使用关联键索引,但随后整个PHP行构造方法都会分崩离析,因为它依赖于数字键。
另一种可以解决您问题的方法是我注意到您提供的数据包含的额外行比图中的数据要多。在这个额外的行中,而不是丢失数据,这些值实际上是null
插入的,所以如果你清除了第一个记录,那么只要以相同的方式创建新数据,你的脚本就应该工作(通过插入空值)。然而,那么你的&#34; N / A&#34;列永远不会被填充,因为它显示为空白单元格。
您按原样编写脚本:
<?php
$cheads = Array (
'Last Name' => Array (
'0' => Array ( 'Field' => 'Last Name', 'Data' => 1, 'User' => 5 ),
'1' => Array ( 'Field' => 'Last Name', 'Data' => 1, 'User' => 5 ),
'2' => Array ( 'Field' => 'Last Name', 'Data' => 1, 'User' => 5 ),
'3' => Array ( 'Field' => 'Last Name', 'Data' => 'Yo', 'User' => 5 ),
'4' => Array ( 'Field' => 'Last Name', 'Data' => 'Test', 'User' => 5 )
),
'Phone Name' => Array (
'0' => Array ( 'Field' => 'Phone Name', 'Data' => 2, 'User' => 5 ),
'1' => Array ( 'Field' => 'Phone Name', 'Data' => 2, 'User' => 5 ),
'2' => Array ( 'Field' => 'Phone Name', 'Data' => 2, 'User' => 5 ),
'3' => Array ( 'Field' => 'Phone Name', 'Data' => 'Yo', 'User' => 5 ),
'4' => Array ( 'Field' => 'Phone Name', 'Data' => null, 'User' => 5 )
),
'Address Name' => Array (
'0' => Array ( 'Field' => 'Address Name', 'Data' => 3, 'User' => 5 ),
'1' => Array ( 'Field' => 'Address Name', 'Data' => 3, 'User' => 5 ),
'2' => Array ( 'Field' => 'Address Name', 'Data' => 'Yo', 'User' => 5 ),
'3' => Array ( 'Field' => 'Address Name', 'Data' => null, 'User' => 5 )
),
'Description' => Array (
'0' => Array ( 'Field' => 'Description', 'Data' => 4, 'User' => 5 ),
'1' => Array ( 'Field' => 'Description', 'Data' => 4, 'User' => 5 ),
'2' => Array ( 'Field' => 'Description', 'Data' => 4, 'User' => 5 ),
'3' => Array ( 'Field' => 'Description', 'Data' => 'Yo', 'User' => 5 ),
'4' => Array ( 'Field' => 'Description', 'Data' => null, 'User' => 5 )
),
'Fruit Type' => Array (
'0' => Array ( 'Field' => 'Fruit Type', 'Data' => 'Peanuts', 'User' => 5 ),
'1' => Array ( 'Field' => 'Fruit Type', 'Data' => 'Apple', 'User' => 5 )
)
);
$keys = ['Last Name', 'Phone Name', 'Address Name', 'Description', 'Fruit Type'];
?>
<table border=1>
<thead>
<tr>
<?php
foreach ($keys as $key)
echo "<th>$key</th>";
?>
</tr>
</thead>
<tbody>
<?php
for($i = 0; $i < count($cheads[$keys[0]]); $i++){ // foreach 'Last Name'
echo "<tr>";
for($k = 0; $k < count($keys); $k++){ // foreach $keys
echo "<td>";
if(array_key_exists($i, $cheads[$keys[$k]])){
echo $cheads[$keys[$k]][$i]['Data'];
}else{
echo "N/A";
}
echo "</td>";
}
echo "</tr>\n";
}
?>
</tbody>
</table>
<table border=1>
<thead>
<tr>
<th>Last Name</th><th>Phone Name</th><th>Address Name</th><th>Description</th><th>Fruit Type</th> </tr>
</thead>
<tbody>
<tr><td>1</td><td>2</td><td>3</td><td>4</td><td>Peanuts</td></tr>
<tr><td>1</td><td>2</td><td>3</td><td>4</td><td>Apple</td></tr>
<tr><td>1</td><td>2</td><td>Yo</td><td>4</td><td>N/A</td></tr>
<tr><td>Yo</td><td>Yo</td><td></td><td>Yo</td><td>N/A</td></tr>
<tr><td>Test</td><td></td><td>N/A</td><td></td><td>N/A</td></tr>
</tbody>
</table>
&#13;
使用由同步数字键构造的数组时。
$cheads = Array (
'Last Name' => Array (
'0' => Array ( 'Field' => 'Last Name', 'Data' => 1, 'User' => 5 ),
'1' => Array ( 'Field' => 'Last Name', 'Data' => 1, 'User' => 5 ),
'2' => Array ( 'Field' => 'Last Name', 'Data' => 1, 'User' => 5 ),
'3' => Array ( 'Field' => 'Last Name', 'Data' => 'Yo', 'User' => 5 ),
'4' => Array ( 'Field' => 'Last Name', 'Data' => 'Test', 'User' => 5 )
),
'Phone Name' => Array (
'0' => Array ( 'Field' => 'Phone Name', 'Data' => 2, 'User' => 5 ),
'1' => Array ( 'Field' => 'Phone Name', 'Data' => 2, 'User' => 5 ),
'2' => Array ( 'Field' => 'Phone Name', 'Data' => 2, 'User' => 5 ),
'3' => Array ( 'Field' => 'Phone Name', 'Data' => 'Yo', 'User' => 5 ),
'4' => Array ( 'Field' => 'Phone Name', 'Data' => null, 'User' => 5 )
),
'Address Name' => Array (
'0' => Array ( 'Field' => 'Address Name', 'Data' => 3, 'User' => 5 ),
'1' => Array ( 'Field' => 'Address Name', 'Data' => 3, 'User' => 5 ),
'3' => Array ( 'Field' => 'Address Name', 'Data' => 'Yo', 'User' => 5 ),
'4' => Array ( 'Field' => 'Address Name', 'Data' => null, 'User' => 5 )
),
'Description' => Array (
'0' => Array ( 'Field' => 'Description', 'Data' => 4, 'User' => 5 ),
'1' => Array ( 'Field' => 'Description', 'Data' => 4, 'User' => 5 ),
'2' => Array ( 'Field' => 'Description', 'Data' => 4, 'User' => 5 ),
'3' => Array ( 'Field' => 'Description', 'Data' => 'Yo', 'User' => 5 ),
'4' => Array ( 'Field' => 'Description', 'Data' => null, 'User' => 5 )
),
'Fruit Type' => Array (
'3' => Array ( 'Field' => 'Fruit Type', 'Data' => 'Peanuts', 'User' => 5 ),
'4' => Array ( 'Field' => 'Fruit Type', 'Data' => 'Apple', 'User' => 5 )
)
);
<table border=1>
<thead>
<tr>
<th>Last Name</th><th>Phone Name</th><th>Address Name</th><th>Description</th><th>Fruit Type</th> </tr>
</thead>
<tbody>
<tr><td>1</td><td>2</td><td>3</td><td>4</td><td>N/A</td></tr>
<tr><td>1</td><td>2</td><td>3</td><td>4</td><td>N/A</td></tr>
<tr><td>1</td><td>2</td><td>N/A</td><td>4</td><td>N/A</td></tr>
<tr><td>Yo</td><td>Yo</td><td>Yo</td><td>Yo</td><td>Peanuts</td></tr>
<tr><td>Test</td><td></td><td></td><td></td><td>Apple</td></tr>
</tbody>
</table>
&#13;
答案 1 :(得分:0)
诀窍是改变我在数组中获取数据的方式。我使用不同方法的关联数组。
之前:
cheads: Array (
[Last Name] => Array (
[0] => Array ( [Field] => Last Name [Data] => 1 [User] => 5 )
[1] => Array ( [Field] => Last Name [Data] => 1 [User] => 5 )
[2] => Array ( [Field] => Last Name [Data] => 1 [User] => 5 )
[3] => Array ( [Field] => Last Name [Data] => Yo [User] => 5 )
[4] => Array ( [Field] => Last Name [Data] => Test [User] => 5 ) )
[Phone Name] => Array (
[0] => Array ( [Field] => Phone Name [Data] => 2 [User] => 5 )
[1] => Array ( [Field] => Phone Name [Data] => 2 [User] => 5 )
[2] => Array ( [Field] => Phone Name [Data] => 2 [User] => 5 )
[3] => Array ( [Field] => Phone Name [Data] => Yo [User] => 5 )
[4] => Array ( [Field] => Phone Name [Data] => [User] => 5 ) )
[Address Name] => Array (
[0] => Array ( [Field] => Address Name [Data] => 3 [User] => 5 )
[1] => Array ( [Field] => Address Name [Data] => 3 [User] => 5 )
[2] => Array ( [Field] => Address Name [Data] => Yo [User] => 5 )
[3] => Array ( [Field] => Address Name [Data] => [User] => 5 ) )
[Description] => Array (
[0] => Array ( [Field] => Description [Data] => 4 [User] => 5 )
[1] => Array ( [Field] => Description [Data] => 4 [User] => 5 )
[2] => Array ( [Field] => Description [Data] => 4 [User] => 5 )
[3] => Array ( [Field] => Description [Data] => Yo [User] => 5 )
[4] => Array ( [Field] => Description [Data] => [User] => 5 ) )
[Fruit Type] => Array (
[0] => Array ( [Field] => Fruit Type [Data] => Peanuts [User] => 5 )
[1] => Array ( [Field] => Fruit Type [Data] => Apple [User] => 5 ) ) )
后:
Matrix: Array (
[D2AA0FA4-4F91-4757-9D27-9F65E0390EF1] => Array ( [Last Name] => 1 [Phone Name] => 1 [Address Name] => 1 [Description] => 1 )
[182EDD25-89CE-40F0-87AE-53D218CD0A1F] => Array ( [Last Name] => 2 [Phone Name] => 2 [Address Name] => 2 [Description] => 2 )
[59EF4DA3-0D3B-4F37-A415-10DD414351A7] => Array ( [Last Name] => 3 [Phone Name] => 3 [Address Name] => 3 [Description] => 3 [Fruit Type] => Apple )
[1C7B67E2-9230-4215-B990-C366077DA6C3] => Array ( [Last Name] => 5 [Phone Name] => 5 [Address Name] => 5 [Description] => 5 ) )
我有一个额外的标识符字段,用于按用户标识每个数据。我将它们作为父键,并为每个标识符添加了所有字段数据,并为每个子数据创建字段类型键。
在此之前,我收集了另一个数组中的所有键,我遍历它并简单地回应如下:
foreach ($matrix as $key => $value) {
echo "<tr>";
for($i = 0; $i < count($cheads); $i++){
$head = $cheads[$i];
if(isset($value[$head])){
echo "<td>";
echo $value[$head];
echo "</td>";
}else{
echo "<td>";
echo "NA";
echo "</td>";
}
}
echo "<td><a href='?action=edit&guid=$key'>Edit</a></td>";
echo "<tr>";
}
结果:
使用关联键和不同的方法为我做了。
感谢。