0

When I do the foreach loop for the print_r output I can succesfully output $key but when outputting $cats the output is 'array'. What index do I need to pass to the varibale $cats[??]. Do I need another foreach loop in View or do I need to reorganize the array in controller? (which I tried both without success). As you can tell I am not the master in programming. Thanks for your patience and any direction!

View:

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

Controller:

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);
1
  • Try with var_export($cats, true); Commented Oct 25, 2013 at 10:43

4 Answers 4

2

You could, as you suggest, use a foreach within the existing loop to display all of the $cat options:

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

Alternatively you could "collapse" the $cats array into a single string using join or implode and output that:

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

(Here I've chosen to add a , between each $cats item.

Sign up to request clarification or add additional context in comments.

1 Comment

both options work perfectly. Second option is wonderful. Best thanks for that! Ill accept answer shortly...
2

As it can be seen from your print_r, you have Array containing arrays, so you need to loop through the contained arrays too. For more understanding I will add a declaration of such array below:

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

Depends on the needs you have, you should consider where to put the second foreach, if you need to create <div class="box_body"> or not.

Comments

1

It is because <?='s functionaltly is same as echo so when you try to <?= $cat it gives array. It would be better if you use print_r($cat)

2 Comments

Though your answer is correct regarding the results of echo $arry compared to print_r($arry) (where $arry is obviously of type array) I somehow doubt, that print_r-ing the array for purposes other than "debugging" is a proper solution for the OP's problem.
Aagree..I was not quite certain to what the OP wants to do with $cat and was waiting for his comment..sadly he didn't .
1

Check out var_dump and its related functions in the "see also" section:

PHP.net Resource Guide

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.