我有以下数组$consulta2 = Array ( [AB] => Array ( [0] => Noah [1] => 48 ) [BC] => Array ( [0] => Jacob [1] => 42 ) [DA] => Array ( [0] => Mason [1] => 27 ) )我想在表中打印带有 KEYS 和 VALUES 的数组我目前正在使用这个 foreach 循环foreach ($consulta2 as $key => $value){ foreach ($value as $k => $v) { $template.= '<tr> <td class="service">'.$key.'</td> <td class="desc">'.$v.'</td> \r \n <td class="service">'.$v.'</td> <td class="service"></td> </tr>'; } }我得到下表+-----+-------+-------+| Key | Value | Value |+-----+-------+-------+| AB | Noah | Noah |+-----+-------+-------+| AB | 48 | 48 |+-----+-------+-------+| BC | Jacob | Jacob |+-----+-------+-------+| BC | 42 | 42 |+-----+-------+-------+| DA | Mason | Mason |+-----+-------+-------+| DA | 27 | 27 |+-----+-------+-------+但我希望它看起来像这样+-----+-------+-------+| Key | Value | Value |+-----+-------+-------+| AB | Noah | 48 |+-----+-------+-------+| BC | Jacob | 42 |+-----+-------+-------+| DA | Mason | 27 |+-----+-------+-------+如果有人知道如何解决它,我将非常感激
1 回答
qq_笑_17
TA贡献1818条经验 获得超7个赞
$value删除内循环并通过键 (0和)访问项目1:
foreach ($consulta2 as $key => $value){
$template.= '<tr>
<td class="service">'.$key.'</td>
<td class="desc">'.$value[0].'</td> \r \n
<td class="service">'.$value[1].'</td>
<td class="service"></td>
</tr>';
}
- 1 回答
- 0 关注
- 90 浏览
添加回答
举报
0/150
提交
取消