0

I have an .ini file. This is what an .ini file looks like:

[something]
a = b
c = d
e = f
g = h

I have the following PHP code:

$ini = parse_ini_file("data.ini", true);
print_r($ini);

The output is:

Array
(
    [something] => Array
        (
            [a] => b
            [c] => d
            [e] => f
            [g] => h
        )

)

Is there a way to 'decode' the array? For example, that the output is this:

a => b
c => d
e => f
g => h

Thanks!

4
  • $ini['something']? Looks like parse_ini_file also gives you the sections. Commented Aug 10, 2014 at 20:03
  • @bali182 Notice: Undefined index: something Commented Aug 10, 2014 at 20:04
  • 1
    The array is "decoded" from the INI file and should be used as-is, as a Map (perhaps with iteration of the key/value pairs). $ini["a"] // => "b". See a tutorial/reference, eg. oreilly.com/catalog/progphp/chapter/ch05.html , php.net/manual/en/language.types.array.php Commented Aug 10, 2014 at 20:09
  • Decode what has just been decoded, thats an interesting question. Commented Aug 10, 2014 at 20:18

2 Answers 2

4

Like this :

$ini = parse_ini_file("data.ini", true);


foreach ($ini['something'] as $key => $value) {
    echo $key . " => ". $value."<br />";
}

OUTPUT

a => b
c => d
e => f
g => h
Sign up to request clarification or add additional context in comments.

3 Comments

That works, but I still see Array ( [something] => Array ( [a] => b [c] => d [e] => f [g] => h ) ). After that I just see a => b etc.
@WilliamDavidEdwards, I can't help but I feel that you simply copy/pasted the code above without actually understanding it. Try to figure out what's going on first and then you can actually get the desired output.
@WilliamDavideEdwards Because $ini is an array containing other arrays. Try print_r($ini['something']) you will understand.
0

Its same as mentioned in php page

<?php
$fruit = array('a' => 'apple', 'b' => 'banana', 'c' => 'cranberry');

reset($fruit);
while (list($key, $val) = each($fruit)) {
    echo "$key => $val\n";
}
?>

2 Comments

I don't understand how this works with .ini files.
reset(), list() and each(). R.I.P. foreach.

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.