user1549397
user1549397

Reputation: 651

Is there a way to have multidimensional config arrays?

I want to create my own custom config data in the application section of Codeigniter, but was wondering if we can build two dimensional config arrays.

Example of application/config/myconfig.php:

$myconfigarray = array('row1'=> array( 'col1'=>'col1val',
                                       'col2'=>'col2val',
                                       'col3'=>'col3val' ),
                       'row2'=> array( 'col1'=>'col1val',
                                       'col2'=>'col2val',
                                       'col3'=>'col3val' ));

Currently by default at least, Codeigniter only seems to support one dimensional config arrays. How can I use multidimensional arrays?

Upvotes: 1

Views: 1913

Answers (1)

No Results Found
No Results Found

Reputation: 102745

You can have configuration arrays in any size shape or form. Getting them to work entirely depends on what you want to do with them, how you're loading them, and how you're reading them.

I think your problem is that you need to rename $myconfigarray to $config:

http://codeigniter.com/user_guide/libraries/config.html

Note: If you do create your own config files use the same format as the primary one, storing your items in an array called $config

So for your example, let's say the file is called myconfig.php:

$this->load->config('myconfig');
foreach (config_item('row1') as $k => $v)
{
    echo $k.' = '.$v;
}

This would print:

col1 = col1val
col2 = col2val
col3 = col3val

If you need to access a certain index, you'd have to do something like this:

$item = config_item('row1');
echo $item['col1']; // col1val

If you happen to be running PHP 5.4 you can access it like so:

echo config_item('row1')['col1'];

Upvotes: 1

Related Questions