/ Published in: PHP
Prints an array (recursive) as PHP code (can be pasted into a php file and it will work).
Note: This function can process arrays with integers/strings/sub-arrays. It is impossible to process resources (they have a state), and while it is possible to process objects, I didn't see a need for it. However, if you need it to support objects as well, I'll be more than happy to alter the function. Just drop a comment to let me know.
Note: This function can process arrays with integers/strings/sub-arrays. It is impossible to process resources (they have a state), and while it is possible to process objects, I didn't see a need for it. However, if you need it to support objects as well, I'll be more than happy to alter the function. Just drop a comment to let me know.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
/** * Print an array (recursive) as PHP code (can be pasted into a php file and it will work). * @param array $array * @param boolean $return (whether to return or print the output) * @return string|boolean (string if $return is true, true otherwise) */ function printArrayAsPhpCode($array, $return = false) { if (!$return) { print "array()"; return true; } else { return "array()"; } } $string = "array("; $no_keys = true; foreach ($array as $value) { $string .= "$value, "; $string .= printArrayInPHPFormat($value, true) . ",\n"; $string .= "$value', "; } else { } } } else { $string .="\n"; foreach ($array as $key => $value) { $no_keys = false; $string .= "\"$key\" => $value,\n"; $string .= "\"$key\" => " . printArrayInPHPFormat($value, true) . ",\n"; $string .= "\"$key\" => '$value',\n"; } else { } } } if (!$no_keys) { $string .= "\n"; } $string .= ")"; if (!$return) { print $string; return true; } else { return $string; } }
URL: http://terraduo.com