Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php ini creating arrays with parse_ini_file

Tags:

php

ini

Is it possible to create an array with a settings file?

In the index.php file there it reads .ini file:

// Parse config file
$settings = parse_ini_file("settings");

E.g. Settings file looks like this:

[States]
east = "Michigan, New York, Minnesota"

Looking to create an array like so:

array('Michigan', 'New York', 'Minnesota')
like image 972
chrisjlee Avatar asked Jun 26 '12 18:06

chrisjlee


2 Answers

The right way to create an array in your ini file is with brackets:

[States]
east[] = Michigan
east[] = New York
east[] = Minnesota

You can see an example in the documentation for parse_ini_file():

like image 197
Wesley Murch Avatar answered Sep 24 '22 08:09

Wesley Murch


It returns an associative array. Then, to parse the east states into an array, you could do: $eastStates = explode(', ', $ini['States']['east']); if your data is indeed in the format you described. Note that you can create true arrays in ini format, see the documentation.

like image 30
Lusitanian Avatar answered Sep 24 '22 08:09

Lusitanian