如何在PHP/CakePHP中将关联数组转换为一组HTML表行?
我的数组是打印的如何在PHP/CakePHP中将关联数组转换为一组HTML表行?,php,arrays,cakephp,Php,Arrays,Cakephp,我的数组是打印的$otaDetails Array ( [0] => Array ( [OtherOtaBooking] => Array ( [id] => 1 [listing_id] => 0 [event_id] => 123testing [first_name] => [last_name] => [email_id] => [phone_number] => 0 [title] => [checkin] => 000
$otaDetails
Array ( [0] => Array ( [OtherOtaBooking] => Array ( [id] => 1 [listing_id] => 0 [event_id] => 123testing [first_name] => [last_name] => [email_id] => [phone_number] => 0 [title] => [checkin] => 0000-00-00 [checkout] => 0000-00-00 [no_of_guests] => 0 [amount_paid] => 0 [amount_rec] => 0 [reservation_id] => 0 [booking_src] => ) ) [1] => Array ( [OtherOtaBooking] => Array ( [id] => 2 [listing_id] => 0 [event_id] => 123testing [first_name] => testamar 12 [last_name] => [email_id] => aaaaa@gmail.com [phone_number] => 2147483647 [title] => CasaMelhor: 2BHK in Candolim:CM001 [checkin] => 2015-12-16 [checkout] => 2015-12-17 [no_of_guests] => 21 [amount_paid] => 2147483647 [amount_rec] => 111111 [reservation_id] => 0 [booking_src] => asdddddddd ) )
我想把它打印成表格格式
<?php
foreach ($otaDetails as $otaDetail){
echo'<tbody>';
echo'<tr>';
echo'<td>'. $otaDetail['id']."</td>";
echo'<td>'. $otaDetail['listing_id'].'</td>';
echo'<td>'. $otaDetail['first_name'].'</td>';
echo'<td>'. $otaDetail['last_name'].'</td>';
echo'<td>'. $otaDetail['email_id'].'</td>';
echo'<td>'. $otaDetail['phone_number'].'</td>';
echo'<td>'. $otaDetail['title'].'</td>';
echo'<td>'. $otaDetail['checkin'].'</td>';
echo'<td>'. $otaDetail['checkout'].'</td>';
echo'<td>'. $otaDetail['no_of_guests'].'</td>';
echo'<td>'. $otaDetail['amount_paid'].'</td>';
echo'<td>'. $otaDetail['amount_rec'].'</td>';
echo'<td>'. $otaDetail['reservation_id'].'</td>';
echo'<td>'. $otaDetail['booking_src'].'</td>';
echo'<tr>';
echo'</tbody>';
}
?>
但是,这会产生以下错误:-
未定义索引:id[APP\Plugin\PropManagement\View\Otherotabookings\index.ctp,第59行]
检查您最后的
标签。关闭它
您的问题是数组是多维的,并且在迭代父数组时试图引用子数组的键 试试这个:
<?php foreach ($otaDetails as $otaDetail){
echo'<tr>';
echo'<td>'. $otaDetail['OtherOtaBooking']['id']."</td>";
echo'<td>'. $otaDetail['OtherOtaBooking']['listing_id'].'</td>';
echo'<td>'. $otaDetail['OtherOtaBooking']['first_name'].'</td>';
echo'<td>'. $otaDetail['OtherOtaBooking']['last_name'].'</td>';
echo'<td>'. $otaDetail['OtherOtaBooking']['email_id'].'</td>';
echo'<td>'. $otaDetail['OtherOtaBooking']['phone_number'].'</td>';
echo'<td>'. $otaDetail['OtherOtaBooking']['title'].'</td>';
echo'<td>'. $otaDetail['OtherOtaBooking']['checkin'].'</td>';
echo'<td>'. $otaDetail['OtherOtaBooking']['checkout'].'</td>';
echo'<td>'. $otaDetail['OtherOtaBooking']['no_of_guests'].'</td>';
echo'<td>'. $otaDetail['OtherOtaBooking']['amount_paid'].'</td>';
echo'<td>'. $otaDetail['OtherOtaBooking']['amount_rec'].'</td>';
echo'<td>'. $otaDetail['OtherOtaBooking']['reservation_id'].'</td>';
echo'<td>'. $otaDetail['OtherOtaBooking']['booking_src'].'</td>';
echo'<tr>';
}
?>
当然,如果您有多个子数组,只需在现有循环中运行一个
foreach
循环。用此行替换代码行
echo“”$otaDetail['OtherOtaBooking']['id']。“”
你会得到你的答案
你忘了再添加一个索引,它是[OtherOtaBooking]试试我的代码它的工作$otaDetail是一个多维数组,所以你需要像
$otaDetail['OtherOtaBooking'][index]
<?php foreach ($otaDetails as $otaDetail){
echo'<tr>';
echo'<td>'. $otaDetail['OtherOtaBooking']['id']."</td>";
echo'<td>'. $otaDetail['OtherOtaBooking']['listing_id'].'</td>';
echo'<td>'. $otaDetail['OtherOtaBooking']['first_name'].'</td>';
echo'<td>'. $otaDetail['OtherOtaBooking']['last_name'].'</td>';
echo'<td>'. $otaDetail['OtherOtaBooking']['email_id'].'</td>';
echo'<td>'. $otaDetail['OtherOtaBooking']['phone_number'].'</td>';
echo'<td>'. $otaDetail['OtherOtaBooking']['title'].'</td>';
echo'<td>'. $otaDetail['OtherOtaBooking']['checkin'].'</td>';
echo'<td>'. $otaDetail['OtherOtaBooking']['checkout'].'</td>';
echo'<td>'. $otaDetail['OtherOtaBooking']['no_of_guests'].'</td>';
echo'<td>'. $otaDetail['OtherOtaBooking']['amount_paid'].'</td>';
echo'<td>'. $otaDetail['OtherOtaBooking']['amount_rec'].'</td>';
echo'<td>'. $otaDetail['OtherOtaBooking']['reservation_id'].'</td>';
echo'<td>'. $otaDetail['OtherOtaBooking']['booking_src'].'</td>';
echo'</tr>';
}
?>
如果您使用的是CakePHP(如您所述),您可以通过发出以下命令获得相同的结果:
<tbody>
<?=$this->Html->tableCells(Hash::extract($otaDetails,'{n}.OtherOtaBooking'))?>
</tbody>
您可以使用
$this->OtherOtaBooking->find()
中的$fields
选项筛选要打印的列,除了应该在foreach之外,是否有任何错误?但给出以下错误未定义的索引:id[APP\Plugin\PropManagement\View\Otherotabookings\index.ctp,第59行]你有一个三维数组,但你把它当作一个二维数组。要得到第一个ID,你必须做0=>OtherOtaBooking=>ID。你做的是0=>ID。有巨大的缩进是什么?这不是问题所在。它给我未定义的偏移量:0它给我未定义的偏移量:0Doh。我的错。我编辑了答案这是最优雅的方式!
<tbody>
<?php foreach ($otaDetails as $otaDetail){
echo'<tr>';
echo'<td>'. $otaDetail['OtherOtaBooking']['id']."</td>";
echo'<td>'. $otaDetail['OtherOtaBooking']['listing_id'].'</td>';
echo'<td>'. $otaDetail['OtherOtaBooking']['first_name'].'</td>';
echo'<td>'. $otaDetail['OtherOtaBooking']['last_name'].'</td>';
echo'<td>'. $otaDetail['OtherOtaBooking']['email_id'].'</td>';
echo'<td>'. $otaDetail['OtherOtaBooking']['phone_number'].'</td>';
echo'<td>'. $otaDetail['OtherOtaBooking']['title'].'</td>';
echo'<td>'. $otaDetail['OtherOtaBooking']['checkin'].'</td>';
echo'<td>'. $otaDetail['OtherOtaBooking']['checkout'].'</td>';
echo'<td>'. $otaDetail['OtherOtaBooking']['no_of_guests'].'</td>';
echo'<td>'. $otaDetail['OtherOtaBooking']['amount_paid'].'</td>';
echo'<td>'. $otaDetail['OtherOtaBooking']['amount_rec'].'</td>';
echo'<td>'. $otaDetail['OtherOtaBooking']['reservation_id'].'</td>';
echo'<td>'. $otaDetail['OtherOtaBooking']['booking_src'].'</td>';
echo'</tr>';
}
?>
</tbody>
<tbody>
<?=$this->Html->tableCells(Hash::extract($otaDetails,'{n}.OtherOtaBooking'))?>
</tbody>