note 53418 deleted from function.implode by hholzgra
| From: | hholzgra@php.net | Date: | Thu, 02 Aug 2012 13:16:40 +0000 |
| Subject: | note 53418 deleted from function.implode by hholzgra | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-189724@lists.php.net to get a copy of this message | ||
Note Submitter: sam dot bledsoe at nosp at nn dot gmail dot com
----
The function below recursively outputs an array in a format condusive to parsing it in php or
another scripting language. It does NOT output the name of the original array, for that see note 1.
It handles all the cases I could think of elegantly. Comments and criticisms are welcome.
For an array constructed with:
$arr = array("foo" => array('bar' => array(0 => "value 0", 1
=> "value 1")), "foo two" => array(0 => array("bar" =>
"value2")));
The line below:
echo implode_parseable("=", ";<br>$", $arr, "$", ";");
Will produce:
$foo["bar"][0]="value 0";
$foo["bar"][1]="value 1";
$foo_two[0]["bar"]="value2";
NOTES:
1) If the leading identifier on a line is a number, the output will most likely be unusable since
variable names cannot begin with numbers. You can get around this by doing something like:
$arr = array('arr' => $arr);
This will output the array as it actually is (because the key is the same name as the array) instead
of just its fields.
2) Since spaces are not allowed in variable names, they are replaced in lines' leading
identifiers by the $space_replace_char parameter, '_' by default.
Hopefully someone will find this useful, if so drop me a line. Credit and thanks go out to the
people who posted their code on this manual page, especially davidpk212 at gmail dot com and
phpWalter at torres dot ws.
function implode_parseable($inner_glue = "=", $outer_glue = "\n", $array = null,
$prefix = "", $suffix = "", $space_replace_char = '_', $skip_empty =
false, $current_loc = "", $recursion_level = 0){
return $prefix . implode_parseable_r($inner_glue, $outer_glue, $array, $space_replace_char,
$skip_empty, $current_loc, $recursion_level) . $suffix;
}
function implode_parseable_r($inner_glue = "=", $outer_glue = "\n", $array =
null, $space_replace_char = '_', $skip_empty = false, $current_loc = "",
$recursion_level = 0)
{
if(is_array($array)){
$output = array();
foreach( $array as $key => $item ){
if ( is_array ($item) ){
//don't quote numeric indicies
if(is_string($key))
$quoted_key = "\"" . $key . "\"";
else
$quoted_key = $key;
// This is value is an array, go and do it again!
$level = $recursion_level + 1;
if($recursion_level == 0){
// can't have spaces in a variable name!
$current_loc .= str_replace(' ', $space_replace_char, $key);
$output[] = implode_parseable_r ($inner_glue, $outer_glue, $item, '_',
$skip_empty, $current_loc, $level);
//start the position tracker over after every run from level 0
$current_loc = '';
}else{
$current_loc .= "[" . $quoted_key . "]";
$output[] = implode_parseable_r ($inner_glue, $outer_glue, $item, '_',
$skip_empty, $current_loc, $level);
//remove the last index from the position tracker string after using it
$current_loc = ereg_replace('\[[^]]*\]$', '', $current_loc);
}
}
else{
// don't quote or []ify the base variable name,
// but do for all else as appropriate
if($recursion_level != 0){
if(is_string($key))
$key = "\"" . $key . "\"";
$key = "[" . $key . "]";
}
// echo "<br>";
// var_dump($item);
// echo "<br>";
$skip_this = false;
if($skip_empty && (!isset($item) || $item == NULL || $item == ''))
$skip_this = true;
//quote the item (which is the value of the array index) if it is a string
if(is_string($item)) $item = "\"" . $item . "\"";
if(!$skip_this) $output[] = $current_loc . $key . $inner_glue . $item;
}
}
return implode($outer_glue, $output);
}else{
return $array;
}
}