13

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')
5
  • 1
    That's exactly what parse_ini_file() returns: an associative array. See here: php.net/manual/en/function.parse-ini-file.php Commented Jun 26, 2012 at 18:25
  • 1
    Are you wanting to create an array that looks like: ("Michigan", "Alaska", "Minnesota") ? Commented Jun 26, 2012 at 18:26
  • @JonathanM yes i'm trying to create an array that looks like (Michigan, Alaska, Minnesota) Commented Jun 26, 2012 at 18:28
  • @chrisjlee, my answer will do that for you -- the explode function takes a delimeted string and converts it to an array Commented Jun 26, 2012 at 18:32
  • Chris: any chance you could change the accepted answer, for the benefit of others (like me) looking up this question? Commented Jun 11, 2014 at 12:41

2 Answers 2

56

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():

Sign up to request clarification or add additional context in comments.

Comments

4

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.

1 Comment

For the OP: Alaskans (as well as folks in the other listed states) will be glad to know they're on the east coast. :) Maybe we're selling Alaska to Russia?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.