获取回显的数组值,而不是单词“array”


Getting array values echoed instead of the word 'array'

当我为print_r输出执行foreach循环时,我可以成功地输出$key但是当输出$cats时,输出是"数组"。我需要将什么索引传递给变量 $cats[??]。我是否需要在视图中使用另一个 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; ?>

Print_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);

您可以按照建议在现有循环中使用 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项目之间添加一个,

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

<?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">

这是因为<?=的功能与echo相同,所以当你尝试<?= $cat时,它会给出array。如果你使用print_r($cat)会更好

在"另请参阅"部分中查看var_dump及其相关函数:

PHP.net 资源指南