php 打印一个数组作为代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5139637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
print an array as code
提问by Ward Bekker
I want to convert a big yaml file to PHP array source code. I can read in the yaml code and get back a PHP array, but with var_dump($array) I get pseudo code as output. I would like to print the array as valid php code, so I can copy paste it in my project and ditch the yaml.
我想将一个大的 yaml 文件转换为 PHP 数组源代码。我可以读入 yaml 代码并取回一个 PHP 数组,但是使用 var_dump($array) 我得到伪代码作为输出。我想将数组打印为有效的 php 代码,因此我可以将其复制粘贴到我的项目中并丢弃 yaml。
回答by deceze
You're looking for var_export
.
您正在寻找var_export
.
回答by GordonM
You could use var_export, serialize(with unserialize on the reserving end), or even json_encode(and use json_decode on the receiving end). The last one has the advantage of producing output that can be processed by anything that can handle JSON.
您可以使用var_export、serialize(在保留端反序列化),甚至json_encode(并在接收端使用 json_decode)。最后一个的优点是生成的输出可以被任何可以处理 JSON 的东西处理。
回答by Dieter Gribnitz
Don't know why but I could not find satisfying code anywhere.
不知道为什么,但我在任何地方都找不到令人满意的代码。
Quickly wrote this. Let me know if you find any errors.
赶紧写了这篇。如果您发现任何错误,请告诉我。
function printCode($array, $path=false, $top=true) {
$data = "";
$delimiter = "~~|~~";
$p = null;
if(is_array($array)){
foreach($array as $key => $a){
if(!is_array($a) || empty($a)){
if(is_array($a)){
$data .= $path."['{$key}'] = array();".$delimiter;
} else {
$data .= $path."['{$key}'] = \"".htmlentities(addslashes($a))."\";".$delimiter;
}
} else {
$data .= printCode($a, $path."['{$key}']", false);
}
}
}
if($top){
$return = "";
foreach(explode($delimiter, $data) as $value){
if(!empty($value)){
$return .= '$array'.$value."<br>";
}
};
return $return;
}
return $data;
}
//REQUEST
$x = array('key'=>'value', 'key2'=>array('key3'=>'value2', 'key4'=>'value3', 'key5'=>array()));
echo printCode($x);
//OUTPUT
$array['key'] = 'value';
$array['key2']['key3'] = 'value2';
$array['key2']['key4'] = 'value3';
$array['key2']['key5'] = array();
Hope this helps someone.
希望这可以帮助某人。
回答by ZecKa
An other way to display array as code with indentation.
另一种将数组显示为带有缩进的代码的方法。
Tested only with an array who contain string, integer and array.
仅使用包含字符串、整数和数组的数组进行测试。
function bo_print_nice_array($array){
echo '$array=';
bo_print_nice_array_content($array, 1);
echo ';';
}
function bo_print_nice_array_content($array, $deep=1){
$indent = '';
$indent_close = '';
echo "[";
for($i=0; $i<$deep; $i++){
$indent.=' ';
}
for($i=1; $i<$deep; $i++){
$indent_close.=' ';
}
foreach($array as $key=>$value){
echo "<br>".$indent;
echo '"'.$key.'" => ';
if(is_string($value)){
echo '"'.$value.'"';
}elseif(is_array($value)){
bo_print_nice_array_content($value, ($deep+1));
}else{
echo $value;
}
echo ',';
}
echo '<br>'.$indent_close.']';
}