PHP array split at brackets into new Multidimensional array [duplicate]

1 day ago 3
ARTICLE AD BOX

Not to fear, parsing an .ini file is a standard method. (See parse_ini_file in the php docs).

Using your file as the base of this example:

myini.ini

[STRINGS] mystring = fooooo value = foo_bar

test.php

$ini_array = parse_ini_file("myini.ini"); print_r($ini_array); # prints the entire parsed .ini file print($ini_array['mystring']); #prints "fooooo"

Note that by default parse_ini_file ignores sections and gloms all ini settings into the same object. If you'd like to have things scoped sectionally as in your python example, pass true for the process_sections parameter (second parameter).

test2.php

$ini_array = parse_ini_file("myini.ini", true /* will scope sectionally */); print($ini_array['mystring']); #prints nothing print($ini_array['STRINGS']['mystring']); #prints fooooo

answered Jan 15, 2016 at 15:11

mjk's user avatar

10 Comments

This does not help me because it prints all values, i only want to store a single value (mystring) into a variable.

2016-01-15T15:12:50.443Z+00:00

2016-01-15T15:13:27.857Z+00:00

Also, I added an example of how to process ini files by sections since PHP does not do that by default.

2016-01-15T15:18:04.987Z+00:00

Thank you! I wonder why this is not included in the official documentation? Perhaps i missed it.

2016-01-15T15:18:08.883Z+00:00

2016-01-15T15:20:21.44Z+00:00

From the intermediate approach, to get match section, it needs to read any INI file from line to line, in this case you can use fgets function or stream_get_line function to read for each line until the section founded or end of file (not founded).

Inside every line we can use preg_match (using regex pattern) that match with the actual section name been searched.

The essential of this approach is to reduce memory usage meanwhile many concurrent request occurs at the same time, so in this case we use string object with any sufficient length as a buffer.

Good luck Buddy.

answered Dec 27, 2019 at 20:27

OO7's user avatar

The simplest way to read a single value from php.ini into a variable is with the ini_get() function like this:

$php_ini_option_name = 'error_reporting'; $php_ini_option_value = ini_get($php_ini_option_name); echo "PHP.ini option '" . $php_ini_option_name . "' = " . $php_ini_option_value;

This will return the string value of the configuration option on success. According to the docs it will print an empty string for NULL values, or FALSE if the option doesn't exist in php.ini but that doesn't match my experience. From memory if the value is set as 'On' in php.ini ini_get will return '1'. This would lead one to expect that a value set to 'Off' would return '0' but it usually returned a blank string for me. Also, non existent option names usually return a blank string. I don't remember dealing with NULL so your mileage may vary on that. Basically you need to be aware of and prepared to handle what your system returns for special cases.

ini_get should be available in all versions of PHP >= 4.

answered Jun 3, 2023 at 0:58

Night Owl's user avatar

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Read Entire Article