How can I transform a INI
file into a array
into PHP?
Example:
[database]
host = localhost
password = sabe_de_nada_inocente
port = 3306
How can I transform a INI
file into a array
into PHP?
Example:
[database]
host = localhost
password = sabe_de_nada_inocente
port = 3306
If you use parse_ini_file
it will interpret without the sessions, 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);
then you have this:
Array (
[database] => Array (
[host] => localhost
[password] => sabe_de_nada_inocente
[port] => 3306
)
)
If the content comes from a string then use parse_ini_string
, like this:
<?php
$data = '[database]
host = localhost
password = sabe_de_nada_inocente
port = 3306';
print_r(parse_ini_string($data, true));
PHP 5.3.0
Added the third parameter, scanner_mode
, single quotes (apostrophes) are used to set the variable and are no longer part of their value.
Hashes # should not be used for comments, use ;
, using the hash will cause a warning, as it is deprecated, even though parse_ini_*
still interprets the hash as comment.
PHP 5.6.1
Added mode INI_SCANNER_TYPED
, in the third parameter.
PHP 7.0.0
Hashes are finally no longer used as comments and cause a parse error if you try to use them.
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 to 1 or empty, similar to INI_SCANNER_RAW
:
baz="true"
bar="false"
foobar= "On"
foobaz="Off"
testnumer="0"
If you use INI_SCANNER_RAW
(PHP 5.3.0 +)
<?php
var_dump(parse_ini_file('meu.ini', true, INI_SCANNER_RAW));
It will not parse from On, Off, true and false, will all string "literals":
array(1) {
["foo"]=>
array(4) {
["baz"]=>
string(4) "true"
["bar"]=>
string(5) "false"
["foobar"]=>
string(2) "On"
["foobaz"]=>
string(3) "Off"
}
}
If you 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)
}
}
Use the parse_ini_file () function. If there are 'fields' with the same name, the last one will enter as key in the array. To avoid this situation, pass true
as the 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);
Output:
Array
(
[database] => Array
(
[host] => localhost
[password] => sabe_de_nada_inocente
[port] => 3306
)
[ftp] => Array
(
[user] => admin
[password] => admin1234
)
)
Without informing the second argument, the return is:
Array
(
[host] => localhost
[password] => admin1234
[port] => 3306
[user] => admin
)