1

I'm using parse_ini_file() to read an ini file that has this line:

[admin]
hide_fields[] = ctr_ad_headerImg

the problem is that it outputs it like,

[admin]
hide_fields = Array

can someone help me out? how do I read "hide_fields[]" like a string?

Best Regards Joricam

My code is:

$ini_array = parse_ini_file($config_path, true);
//print_r($ini_array);
//echo $ini_array["default_colors"]["sitebg"];
$ini_array["default_colors"]["sitebg"]="#000000";
write_php_ini($ini_array,$config_path);

Functions that Im using:

function write_php_ini($array, $file)
{
    $res = array();
    foreach($array as $key => $val)
    {
        if(is_array($val))
        {
            $res[] = "[$key]";
            foreach($val as $skey => $sval) $res[] = "$skey = ".(is_numeric($sval) ? $sval : ''.$sval.'');
        }
        else $res[] = "$key = ".(is_numeric($val) ? $val : ''.$val.'');
    }
    safefilerewrite($file, implode("\r\n", $res));
}
//////
function safefilerewrite($fileName, $dataToSave)
{    if ($fp = fopen($fileName, 'w'))
    {
        $startTime = microtime();
        do
        {            $canWrite = flock($fp, LOCK_EX);
           // If lock not obtained sleep for 0 - 100 milliseconds, to avoid collision and CPU load
           if(!$canWrite) usleep(round(rand(0, 100)*1000));
        } while ((!$canWrite)and((microtime()-$startTime) < 1000));

        //file was locked so now we can store information
        if ($canWrite)
        {            fwrite($fp, $dataToSave);
            flock($fp, LOCK_UN);
        }
        fclose($fp);
    }

}

1 Answer 1

2

Parse_ini_file() does handle such identifiers. It correctly converts them into arrays on reading the ini file:

 print_r(parse_ini_string("hide_fields[] = ctr_ad_headerImg"));

Will generate:

Array
(
    [hide_fields] => Array
        (
            [0] => ctr_ad_headerImg
        )

The entry can be accessed as $cfg["hide_fields"][0] in PHP. The problem is that the ini file output function you have chosen this time does not understand array attributes.

Since you are probably interested in workarounds instead of using an appropriate tool, apply this conversion loop on your ini data:

// foreach ($sections ...) maybe

foreach ($cfg as $key=>$value) {
    if (is_array($value)) {
         foreach ($value as $i=>$v) {
             $cfg["$key"."[$i]"] = $v;
         }
         unset($cfg[$key]);
    }
}

And save it afterwards.


Edited code

function write_php_ini($array, $file)
{
    $res = array();
    foreach($array as $key => $val)
    {
        if(is_array($val))
        {
            $res[] = "[$key]";
            foreach($val as $skey => $sval) {
                if (is_array($sval)) {
                    foreach ($sval as $i=>$v) {
                        $res[] = "{$skey}[$i] = $v";
                    }
                }
                else {
                    $res[] = "$skey = $sval";
                }
            }
        }
        else $res[] = "$key = $val";
    }
    safefilerewrite($file, implode("\r\n", $res));
}

//////
function safefilerewrite($fileName, $dataToSave)
{    
    file_put_contents($fileName, $dataToSave, LOCK_EX);    
}
Sign up to request clarification or add additional context in comments.

6 Comments

this is a great awnser but I'm php blind almost, I'm a real noob, let me edit my original post with my code and functions, please guide me
Only if you promise that this is your very last .ini question.
it is, I promise, after this I'll have the application I was building complete! I also promise to not quit studying php here, I will read on further until I become an expert and fully understand all of whats hapenning with the code. I give you my word!
See rewritten code. It splits up the arrays into key[123]= entries. If you don't want the numeric index, then remove the $i there. Also took the liberty to cut down the pointless file saving function.
IT WORKS FULL OF AWESOMENESSSSSS :D dude thanks you soooo much! Can you suggeste me readings / book to fully understand whats happening with your code?
|

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.