JasonLewis Posted July 28, 2010 Share Posted July 28, 2010 I don't know if the title explains it very well, so here's what I'm trying to achieve. With this script people can change the value of configuration variables on the fly like so: Config::write('General.load', array('plugin1','plugin2'), true); In that example, it changes the General.load config variable to an array, and sets the 3rd param to true, which means that it can be saved. What I'm trying to incorporate is a method the save part, when a user calls Config::save() it loops through any config variables that were written to and which have had the 3rd parameter set to true. It works perfect for booleans, strings, ints, floats etc. But when it comes to arrays, by default, it would just write "Array" to the file, instead of "array('plugin1','plugin2')". So I went ahead and began developing a little script that converts arrays to a string based array. Here is the function: function array_build(&$output, &$array, $indent){ $output .= 'array('; $i = 0; foreach($array as $key => $val){ if(is_string($key)){ $output .= '\'' . $key . '\' => '; } if(is_array($val)){ array_build($output, $array[$key], $indent++); }else{ // Is the value a string, if so quote it. if(is_string($val)){ // Are we on the last iteration. if($i !== count($array) - 1){ // Nope, comma seperate. $output .= '\'' . $val . '\', '; }else{ // Yep, no comma. $output .= '\'' . $val . '\''; } }else{ // Are we on the last iteration. if($i !== count($array) - 1){ // Nope, comma seperate. $output .= $val . ', '; }else{ // Yep, no comma. $output .= $val; } } } // Increment i, check if we are on the last iteration. ++$i; } $output .= ')'; // Place a comma after all parent level elements. if($indent === 1){ $output .= ', '; } } It works, to a degree. If you supply it with something like this: $array_to_string = ''; $array = array( 'welcome', 'to', 'my', array('website') ); array_build($array_to_string, $array, 0); die($array_to_string); It will output: array('welcome', 'to', 'my', array('website')), It nearly worked. However if I input something like this: $array_to_string = ''; $array = array( 'welcome', 'to', 'my', array('website'), array(array('goodbye', 'foo' => 'bar')) ); array_build($array_to_string, $array, 0); die($array_to_string); The output is: array('welcome', 'to', 'my', array('website')array(array('goodbye', 'foo' => 'bar'), )) I've gone over the code, and I'm not quite sure what I can do to fix it. Variable referencing is still a tad complicated for me. If anyone has any ideas on what I've done wrong, let us know. Cheers. Quote Link to comment Share on other sites More sharing options...
trq Posted July 28, 2010 Share Posted July 28, 2010 What exactly do you plan on doing with this string afterwards? Have you looked at using serialize instead? Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted July 28, 2010 Author Share Posted July 28, 2010 Ah, no, haven't really taken a look at serialize. In the config file values are written like so: Config::write('Plugin.load', array('geshi/geshi')); Of course there could be 10 or 100 more plugins that could be set there. So what I'm essentially trying to do is right to the file an array, like the one above is printed. Which is why I was thinking of converting it into a string so I could write it to the file like the one above. Perhaps I should be going about this a different way? UPDATE: I've discovered var_export, which is good, but not as a clean as I had hoped for. But if I can't fix my function, I'll settle for that. Quote Link to comment Share on other sites More sharing options...
trq Posted July 28, 2010 Share Posted July 28, 2010 So what I'm essentially trying to do is right to the file an array, like the one above is printed. Sorry, but that part doesn't make sense? If you want to save an array in a file so you can re-use it later serialize & unserialize are your friends. Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted July 28, 2010 Author Share Posted July 28, 2010 Hm, what I want to do is write an array to a file, but allow it to be human-readable. So that if the end-user were to open the config.php file they could still read it and change it from there as well. var_export makes the array human-readable, and if I write it to the file it's still in the PHP array format, so it can be used. I think I might have to settle for var_export. Saves re-inventing the wheel. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.