How to turn an INI file into an Array?

Asked

Viewed 184 times

5

How can I turn a file INI in a array in PHP?

Example:

[database]
host = localhost
password = sabe_de_nada_inocente
port = 3306

2 answers

6


Use parse_ini_file it will interpret without the sessions, it will be something like:

Array
(
    [host] => localhost
    [password] => sabe_de_nada_inocente
    [port] => 3306
)

But if you apply the second parameter parse_ini_file('arquivo.ini', true); so you’ll have it:

Array (
   [database] => Array (
        [host] => localhost
        [password] => sabe_de_nada_inocente
        [port] => 3306
    )
)

If the content comes from a string then use parse_ini_string, thus:

<?php

$data = '[database]
host = localhost
password = sabe_de_nada_inocente
port = 3306';

print_r(parse_ini_string($data, true));

Some details that have changed:

  • PHP 5.3.0

    Added the third parameter, the scanner_mode, single quotes (apostrophes) are used to set the variable and are no longer part of their value.

    Hashs # should not be used for comments, use ;, using the hash will cause a Warning, as it is in disuse, despite the parse_ini_* still interpret the hash as comment.

  • PHP 5.6.1

    Added the mode INI_SCANNER_TYPED, in the third parameter.

  • PHP 7.0.0

    Finally hashs are no longer used as comments and causes a parse error if you try to use.

Using the scanner_mode

If you have a . ini like this:

[foo]
baz = true
bar = false
foobar = On
foobaz = Off

And do this:

<?php

var_dump(parse_ini_file('meu.ini', true));

Will return this, string with value 1 if true or On, or empty string if Off or false:

array(1) {
  ["foo"]=>
  array(4) {
    ["baz"]=>
    string(1) "1"
    ["bar"]=>
    string(0) ""
    ["foobar"]=>
    string(1) "1"
    ["foobaz"]=>
    string(0) ""
  }
}

Note that if you use quotation marks or apostrophes it will not parse for 1 or empty, similar to INI_SCANNER_RAW:

baz="true"
bar="false"
foobar= "On"
foobaz="Off"
testnumer="0"

Use INI_SCANNER_RAW (PHP 5.3.0+)

<?php

var_dump(parse_ini_file('meu.ini', true, INI_SCANNER_RAW));

You will not parse On, Off, true and false, they will all be string "literal":

array(1) {
  ["foo"]=>
  array(4) {
    ["baz"]=>
    string(4) "true"
    ["bar"]=>
    string(5) "false"
    ["foobar"]=>
    string(2) "On"
    ["foobaz"]=>
    string(3) "Off"
  }
}

Use INI_SCANNER_TYPED (PHP 7) it will treat On, off, true and false as boolean:

<?php

var_dump(parse_ini_file('meu.ini', true, INI_SCANNER_TYPED));

Return this:

array(1) {
  ["foo"]=>
  array(4) {
    ["baz"]=>
    bool(true)
    ["bar"]=>
    bool(false)
    ["foobar"]=>
    bool(true)
    ["foobaz"]=>
    bool(false)
  }
}
  • 1

    Look, this "sections" information was important, huh. + 1

4

Use the function parse_ini_file(). If there are 'fields' with the same name, the latter will enter as a key in the array. To avoid this situation, pass true as second argument, so the array will be separated by sessions.

config.ini

[database]
host = localhost
password = sabe_de_nada_inocente
port = 3306
[ftp]
user = admin
password = admin1234

php:

$arr = parse_ini_file('config.ini', true);

echo "<pre>";
print_r($arr);

Exit:

Array
(
    [database] => Array
        (
            [host] => localhost
            [password] => sabe_de_nada_inocente
            [port] => 3306
        )

    [ftp] => Array
        (
            [user] => admin
            [password] => admin1234
        )

)

Without informing you the second argument the return is:

Array
(
    [host] => localhost
    [password] => admin1234
    [port] => 3306
    [user] => admin
)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.