2

how do i pass a multi diminutional array to a view? controller code

       public function index(){

           data['navs'] =   array( 
            'name' => array('one', 'two', 'three'),
            'link' => array('one', 'two', 'three'));

            $this->load->view('adminView', $data);}

view code

        <?php if ($navs) {
                foreach ($navs as $nav) {
                echo('<li><a href="' . $nav->link . '">' . $nav->name . '</a></li>');
                }
            }?>

3 Answers 3

4

First of all you need to build the array, the right way. It should be something like:

$data['navs'] = array( array(  'name'    =>  'one',
                               'link'    =>  'linkone'),

                       array(  'name'    =>  'two',
                               'link'    =>  'linktwo')    
                    );
$this->load->view('adminView', $data);

Then in your view:

foreach($navs as $n){
  echo "<li><a href='{$n['link']}'>{$n['name']}</a></li>";
}
Sign up to request clarification or add additional context in comments.

Comments

1

Once in the view, refer to your data as array elelements, not object properties (you are passing the array of arrays, not array of objects). Based on your controller, your view code should look like that:

foreach ($navs as $nav) {
    echo('<li><a href="' . $nav['link'] . '">' . $nav['name'] . '</a></li>');
}

However, that won't output the right result because your $nav['link'] and $nav['name'] are two arrays. You'd need to call any of their elements or change controller accordingly.

Comments

-2

how to fetch dynamic array value by using controller of codeigniter in php

Array
(
    [id] => 2
    [name] => Wellness & Spa
    [isSelected] => true
    [subModules] => Array
        (
            [0] => Array
                (
                    [id] => 1
                    [name] => Spa1
                    [isSelected] => true
                    [0] => object:81
                )

            [1] => Array
                (
                    [id] => 2
                    [name] => Spa2
                    [isSelected] => true
                    [0] => object:82
                )

            [2] => Array
                (
                    [id] => 3
                    [name] => Wellness
                    [isSelected] => true
                    [0] => object:83
                )

        )

)

1 Comment

I can't understand what you're saying. Please give some more context.

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.