32

Is there a way to specify a one dimensional array in a ini file.

so in my ini I would like to do

someproperty = [array of something]

I am using Zend_Config_Ini config adapter (I prefer ini for base configuration).

Akeem
  • 7,897
  • 5
  • 32
  • 41

3 Answers3

39
someproperty[] = a
someproperty[] = b
someproperty[] = c
someproperty[] = d
someproperty[] = e

see: http://us.php.net/manual/en/function.parse-ini-file.php#75983

Alister Bulman
  • 34,482
  • 9
  • 71
  • 110
  • 2
    And is it possible to do it for multi dimensional (so someproperty values maybe a hash/multidimensional array) ? – Akeem Dec 03 '08 at 21:44
  • 5
    I came across an issue today where this syntax will not work correctly if a [section] is not defined at the top of the ini file. Not sure if this is a bug, but definitely worth noting! – Tom Aug 18 '11 at 15:12
  • @Tom: thanks for pointing it out, gave me a hard time - I'd definitely consider this a bug! – Select0r Jan 13 '12 at 14:16
7

Although undocumented, this seems to work quite well too:

foo[bar] = 5
foo[baz] = 6
hello[world] = 7
maček
  • 76,434
  • 37
  • 167
  • 198
  • 1
    This works for me too. You'll have to do `$config->get('the_key')->toArray()` to use it as an array in PHP. – Christof Apr 09 '14 at 08:16
  • It's no longer undocumented. Arrays with empty indices and string indices are covered in the examples for `parse_ini_file()`. See: http://us.php.net/manual/en/function.parse-ini-file.php – Mr. Lance E Sloan Jul 13 '15 at 18:56
6

You can use separators to make further sub-sections, and they are presented as either another level of objects ($config->some->a) or with $config->toArray(), they can be turned into a multi-level array.

Combining both the above techniques to make arrays, and the separators like so:

some.a[] = a
some.a[] = b
some.b[] = c

will give the results you are looking for.

array('some' => array('a' => array(0 => 'a',
                                   1 => 'b'),
                      'b' => array(0 => 'c')
                     ));
Alister Bulman
  • 34,482
  • 9
  • 71
  • 110
  • 2
    The dotted syntax won't work for PHP's build in `parse-ini-file`-function. The square-bracket-syntax will only work for one-dimensional arrays. – feeela Sep 19 '11 at 14:32