Hey, While I have been working on a script for jon I wrote this function. What it does, is it takes an array input (of any size and number of sub-arrays), and a variable name, then returns the PHP code that would be used to create that array.
For example, say you have the following array:
PHP Code:
$myarray = array('Item 1', 'Item 2', 'Item 3', 'sub_array' => array('Item 4', 'Item 5'));
you call the function and give it a var name: BuildArrayCode($myarray, 'newarray');
And the function returns the following as a string:
$newarray = array(
0 => 'Item 1',
1 => 'Item 2',
2 => 'Item 3',
'sub_array' => array(
0 => 'Item 4',
1 => 'Item 5'
)
);
With line breaks and indents (for readability more than anything).
This may seem somewhat pointless, but in the script it is used to update a configuration file. It makes it much easier checking all the values for changes and the writing the file again from scratch. All you do is, change the values in the array that need to be changed then use this function to get a new array code...
May be helpful, may be not... Heres the function:
PHP Code:
function BuildArrayCode($array, $varname, $level = 1)
{
$out = '';
foreach($array as $key => $value)
{
$tbs = str_repeat("\t", $level);
$tbs = "\r\n".$tbs;
if(!is_array($value))
{
if(is_numeric($value))
{
if(is_numeric($key))
{
$out .= $tbs.$key.' => '.$value.', ';
}
else
{
$out .= $tbs."'".$key."' => ".$value.', ';
}
}
else
{
if(is_numeric($key))
{
$out .= $tbs.$key." => '".$value."', ";
}
else
{
$out .= $tbs."'".$key."' => '".$value."', ";
}
}
}
else
{
if(is_numeric($key))
{
$out .= $tbs.$key." => array(".BuildArrayCode($value, '', $level+1).$tbs."), ";
}
else
{
$out .= $tbs."'".$key."' => array(".BuildArrayCode($value, '', $level+1).$tbs."), ";
}
}
}
$out = substr_replace($out, "", -2);
if($level == 1)
{
$out = '$'.$varname.' = array('."\n\t".$out.''."\n".');';
}
return $out;
}
City Information site for sale
Today, 03:46 PM in Sites for Sale @ Flippa