0

当我为 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);
4

4 回答 4

2

正如您所建议的,您可以在现有循环中使用 foreach 来显示所有$cat选项:

<?php foreach($categories_sorted as $key => $cats) : ?>
    <div class="box_container">
        <div class="box_head"><?= $key; ?></div>
        <div class="box_body">
        <?php foreach($cats as $item) { echo $item; } ?>
        </div>
    </div>
<?php endforeach; ?>

或者,您可以使用 join 或implode将数组“折叠”$cats成单个字符串并输出:

<?php foreach($categories_sorted as $key => $cats) : ?>
    <div class="box_container">
        <div class="box_head"><?= $key; ?></div>
        <div class="box_body">
        <?php echo implode(', ', $cats); ?>
        </div>
    </div>
<?php endforeach; ?>

(这里我选择,在每个 $cats 项目之间添加一个。

于 2013-10-25T10:43:56.113 回答
2

从您的 print_r 可以看出,您有包含数组的 Array,因此您也需要遍历包含的数组。为了更好地理解,我将在下面添加此类数组的声明:

<?php
$categories_sorted = array(
    'Things to Do' => array(
        0   =>  'Activities and  Attractions',
        1   =>  'Castles',
        2   =>  'Golf',
        3   =>  'Islands and Beaches'
    ),
    'Transportation' => array(
        0   =>  'Airport Transfers',
        1   =>  'Car Leasing',
        2   =>  'Flights',
        3   =>  'Public Transport'
    )
);
?>

<?php foreach($categories_sorted as $key => $cats) : ?>
<div class="box_container">
        <div class="box_head"><?= $key; ?></div>
        <?php foreach($cats as $k => $cat): ?>
        <div class="box_body"><?= $cat; ?></div>
        <?php endforeach; ?>
    </div>
<?php endforeach; ?>

取决于您的需求,您应该考虑将第二个 foreach 放在哪里,如果您需要创建<div class="box_body">

于 2013-10-25T10:49:27.960 回答
1

这是因为当你尝试它时,它<?=的功能是一样的 。如果你使用它会更好echo<?= $catarrayprint_r($cat)

于 2013-10-25T10:41:08.403 回答
1

在“另见”部分查看var_dump及其相关功能:

PHP.net 资源指南

于 2013-10-25T11:03:17.780 回答