phparrayscodeignitermodel-view-controller

How to pass multiple arrays from controller to view in CodeIgniter?


I'm trying to pass two arrays ($a_1 and $a_2) from my controller to my view like so:

$this->load->view('confirm_data_v', $a_1, $a_2);

In my view I want to print the value of one of them doing this:

<p><?php echo $name ?></p>
<p><?php echo $mail ?></p>

when I print each array I get this:

Array
(
    [name] => jon
)
Array
(
    [mail] => blabla@server.com

)

$name is a field inside $a_1 and $mail is a field inside $a_2, but it seems like the view doesn't know where these fields are, I mean, it doesn't know in which array is $name and $mail, whether $a_1 or $a_2. How do I do that?.


Solution

  • You're passing the arrays in an incorrect way. You can only pass one data array as a second parameter while loading the view.

    You could instead put each array in the data array in your controller:

    $data['a_1'] = $a_1;
    $data['a_2'] = $a_2;
    $this->load->view('confirm_data_v', $data);
    

    Then in your view you can access $a_1 and $a_2 as you like

    Name: <?php echo $a_1['name']; ?>
    Email: <?php echo $a_2['mail']; ?>