当我为 print_r 输出执行 foreach 循环时,我可以成功输出 $key,但是当输出 $cats 时,输出是“数组”。我需要将什么索引传递给变量 $cats[??]。我需要在 View 中使用另一个 foreach 循环还是需要在控制器中重新组织数组?(我都尝试过,但都没有成功)。如您所知,我不是编程大师。感谢您的耐心和任何指导!
看法:
<?php foreach($categories_sorted as $key => $cats) : ?>
<div class="box_container">
<div class="box_head"><?= $key; ?></div>
<div class="box_body"><?= $cats; ?></div>
</div>
<?php endforeach; ?>
打印_r:
Array
(
[Things to Do] => Array
(
[0] => Activities and Attractions
[1] => Castles
[2] => Golf
[3] => Islands and Beaches
[4] => Kids and Family
[5] => Landmarks and Architecture
[6] => Museums and Galleries
[7] => Nightlife
[8] => Parks and Gardens
[9] => Shopping
[10] => Sports and Outdoor
[11] => Tours Tracks and Cruises
[12] => Tracks and Trails
[13] => Wellness
)
[Transportation] => Array
(
[0] => Airport Transfers
[1] => Car Leasing
[2] => Flights
[3] => Public Transport
[4] => Taxis
[5] => Transport Lift Ride
[6] => Vehicle Rental
[7] => Vehicle Sales
)
)
控制器:
function categories(){
$this->load->model('array_model');
$category_row = $this->array_model->get_categories_all();
$arr_maincats = array();
foreach($category_row as $row){
if(!isset($arr_maincats[$row['category']]))
$maincats = $row['maincategory'];
$arr_maincats[$maincats][] = $row['category'];
}
$data['categories_sorted'] = $arr_maincats;
$this->load->view('array_view', $data);