以PHP代码的形式输出/打印数组。
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了以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.
/** * 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) . ", "; $string .= "$value', "; } else { } } } else { $string .=" "; foreach ($array as $key => $value) { $no_keys = false; $string .= ""$key" => $value, "; $string .= ""$key" => " . printArrayInPHPFormat($value, true) . ", "; $string .= ""$key" => '$value', "; } else { } } } if (!$no_keys) { $string .= " "; } $string .= ")"; if (!$return) { print $string; return true; } else { return $string; } }
以上是关于以PHP代码的形式输出/打印数组。的主要内容,如果未能解决你的问题,请参考以下文章