Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: reading config.ini to array with file()

Tags:

file

php

ini

My config file looks like this:

title = myTitle;
otherTitle = myOtherTitle;

when I read the file with file(), it creates this array

[0] => title = myTitle;
[1] => otherTitle = myOtherTitle;

and what I want the array to look like is

[title] => myTitle;
[otherTitle] => myOtherTitle;

Am I using the wrong approach her? Should i just read the entire config into a sting and explode it from there?

like image 386
Joseph Carrington Avatar asked Jan 29 '26 08:01

Joseph Carrington


1 Answers

You can use the parse_ini_file function. It's available in PHP 4 and 5.

If your config file looks like this:

one = 1;
five = 5;
animal = BIRD;

The function will return the following associative array:

Array
(
    [one] => 1
    [five] => 5
    [animal] => BIRD
)
like image 64
Ayman Hourieh Avatar answered Jan 30 '26 21:01

Ayman Hourieh