How to pass two arrays to view in codeigniter?

Member

by lily , in category: PHP Frameworks , a year ago

How to pass two arrays to view in codeigniter?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by edmond_brakus , a year ago

@lily 

In CodeIgniter, you can pass two arrays to a view by combining them into a single array and then passing that array to the view. Here's how you can do it:


Controller:

1
2
3
4
5
6
7
8
public function index() {
    $data1 = array('foo' => 'bar', 'hello' => 'world');
    $data2 = array('name' => 'John Doe', 'email' => 'johndoe@example.com');

    $combined_data = array('data1' => $data1, 'data2' => $data2);

    $this->load->view('your_view', $combined_data);
}


View (your_view.php):

1
2
<p><?php echo $data1['foo']; ?></p>
<p><?php echo $data2['name']; ?></p>


In the above example, we first create two arrays $data1 and $data2 in the controller. We then combine these arrays into a single array $combined_data and pass it to the view using the load->view() method.


In the view, we can access the individual elements of the arrays by using the keys 'data1' and 'data2'. This allows us to pass and access multiple arrays in a single view in CodeIgniter.

Related Threads:

How to check two result arrays in codeigniter view?
How to pass an array to the main view in codeigniter?
How to pass an array of arrays to graphql?
How to merge multiple arrays in codeigniter?
How to pass data from a controller to a view in Phalcon?
How to pass controller data to view in laravel?