note 56053 deleted from function.print-r by derick
| From: | derick@php.net | Date: | Thu, 27 Oct 2005 10:16:39 +0000 |
| Subject: | note 56053 deleted from function.print-r by derick | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-97677@lists.php.net to get a copy of this message | ||
Note Submitter: rele at gmx dot de
----
I needed to write generic code and I took the nice print_array function from 05-Jul-2005 03:56 and
tuned it a bit.
Single quotes are escaped now and tabs and new lines have been replaced by a new line after each
array element on level 0.
If you don't want any new lines just start with
$php_code = get_array_php_code($my_array, 1);
function get_array_php_code(&$array, $level = 0) {
$php_code = '';
if(is_array($array)) {
$php_code .= 'Array(';
$ct = 1;
foreach($array as $key => $value) {
$php_code .= (is_numeric($key) ? $key : "'$key'" ) . '=>';
$php_code .= get_array_php_code($value, $level+1);
if ($ct != sizeof($array)) {
$php_code .= ',';
if($level == 0) $php_code .= "\n";
}
++$ct;
}
$php_code .= ")";
} else if(is_numeric($array)) {
$php_code .= $array;
} else if(is_bool($array)) {
$php_code .= $array ? 'TRUE' : 'FALSE';
} else {
$php_code .= "'".str_replace("'","\\'", $array).
"'";
}
return $php_code;
}
With this function you can easily search and replace existing data arrays in other PHP scripts
easily with substr_replace i.e.