Zend Framework

Using Array in Zend Config Ini

All of my projects in Zend Framework uses INI files as configuration files. They were just simple key=value pair. What if we want an array as value? Here, I am going to show how to implement an array in Zend_Config_Ini.

A sample INI file looks like this (aside from Zend Framework specific configurations):

# Floor settings
floor.width = 880
floor.height = 600
floor.marker.width = 25
floor.marker.height = 26
floor.marker.offset = 12

To create an array of values ,we will do something like this:

floor.marker.specialMarkers[] = 101
floor.marker.specialMarkers[] = 102
floor.marker.specialMarkers[] = 103

As usual, we get the configurations at bootstrap then save to registry with this:

		$config = new Zend_Config_Ini(
		    APPLICATION_PATH . '/configs/application.ini', 
		    APPLICATION_ENV
		);
		
		//save to registry
		$registry = Zend_Registry::getInstance();
		$registry->set('config', $config);

And finally access the configuration on our models/controllers. To get the configuration, we will get it from the registry:

# get from the registry
$config = Zend_Registry::get('config');

# to get the floor object
$floor = $config->floor;

# to get the floor width
$floorWidth = $floor->width;

# to get the marker height
$markerHeight = $floor->marker->height;

We stored the config object into the Zend_Registry so that we can access them within our Zend Framework application. The configuration object is a Zend_Config object. To use the array, we can do it like this:

# convert object to array
$specialMarkers = $floot->marker->specialMarkers->toArray();

# use the array
foreach ($specialMarkers as $val)
{
    echo "Special marker value: $val <br />";
}

First, we converted the special marker object into an array then assign it to a variable. Next we use the array into our application.

7 thoughts on “Using Array in Zend Config Ini”

  1. I got a “Fatal error: Call to a member function toArray() on a non-object” on converting $floor->marker->specialMarkers;
    How could it be?

  2. The code is just extracted from my previous project so it may be incomplete. I forgot this line in bootstrap, you should add this:

    //save to registry
    $registry = Zend_Registry::getInstance();
    $registry->set(‘config’, $config);

    Sorry, my syntax highlighting plugin sucks when editing posts, so it will be hard for me to modify my post. So I cannot readily update them. Too bad.

  3. I made another mistake, I had to identify the array section into the .ini file (e.g. [floor-data]). After doing that, I was able to load data and convert it to array.

  4. Not sure what version of ZF you’re using but there is a “bug” that causes this functionality not to work: http://framework.zend.com/issues/browse/ZF-10148

    It is marked as “won’t fix” either, but the work around is to numerically index each item in your array:

    floor.marker.specialMarkers.0 = 101
    floor.marker.specialMarkers.1 = 102
    floor.marker.specialMarkers.2 = 103

Leave a reply

Your email address will not be published. Required fields are marked *