Jump to content

Create a string version of an array.


JasonLewis

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.