How to return multidimensional array in codeigniter?

Member

by rollin , in category: PHP Frameworks , a month ago

How to return multidimensional array in codeigniter?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by kadin , a month ago

@rollin 

In CodeIgniter, you can return a multidimensional array by simply creating the array with nested arrays within it and returning it in your controller function. Here's an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
public function get_data() {
    $data = array(
        array(
            'name' => 'John Doe',
            'age' => 30
        ),
        array(
            'name' => 'Jane Smith',
            'age' => 25
        ),
        array(
            'name' => 'Bob Johnson',
            'age' => 35
        )
    );

    return $data;
}


You can then access this array in your view or any other part of your application. Just call the get_data() function in your controller and it will return the multidimensional array.