Ati*_*din 169 php serialization
我想将数组打印到文件中.
我希望该文件看起来与这样的代码看起来完全相似.
print_r ($abc); 假设$ abc是一个数组.
是否有任何一行解决方案,而不是每个外观常规.
PS - 我目前使用序列化但我想使文件可读,因为序列化数组的可读性非常难.
Gor*_*don 284
无论是var_export或将print_r返回输出,而不是打印出来的.
$b = array (
'm' => 'monkey',
'foo' => 'bar',
'x' => array ('x', 'y', 'z'));
$results = print_r($b, true); // $results now contains output from print_r
Run Code Online (Sandbox Code Playgroud)
然后,您可以节省$results用file_put_contents.或者在写入文件时直接返回:
file_put_contents('filename.txt', print_r($b, true));
Run Code Online (Sandbox Code Playgroud)
Fel*_*ing 52
只是用print_r; )阅读文档:
如果要捕获输出
print_r(),请使用return参数.当此参数设置为时TRUE,print_r()将返回信息而不是打印它.
所以这是一种可能性:
$fp = fopen('file.txt', 'w');
fwrite($fp, print_r($array, TRUE));
fclose($fp);
Run Code Online (Sandbox Code Playgroud)
Sar*_*raz 24
你可以尝试:
$h = fopen('filename.txt', 'r+');
fwrite($h, var_export($your_array, true));
Run Code Online (Sandbox Code Playgroud)
Viv*_*lor 10
然而op需要写入数组,因为它在文件中,我已经登陆此页面来找到一个解决方案,在该解决方案中我可以将数组写入文件,并且稍后可以再次使用php轻松读取。
我通过使用 json_encode 找到了自己的解决方案,因此其他人正在寻找相同的解决方案,这里是代码:
file_put_contents('array.tmp', json_encode($array));
Run Code Online (Sandbox Code Playgroud)
比读
$array = file_get_contents('array.tmp');
$array = json_decode($array,true);
Run Code Online (Sandbox Code Playgroud)
你可以试试这个,$myArray因为数组
$filename = "mylog.txt";
$text = "";
foreach($myArray as $key => $value)
{
$text .= $key." : ".$value."\n";
}
$fh = fopen($filename, "w") or die("Could not open log file.");
fwrite($fh, $text) or die("Could not write file!");
fclose($fh);
Run Code Online (Sandbox Code Playgroud)
快速而简单的做到这一点:
file_put_contents($filename, var_export($myArray, true));
Run Code Online (Sandbox Code Playgroud)
我只是写了这个函数来输出一个数组作为文本:
应该输出格式良好的数组。
重要的提示:
注意用户输入。
此脚本是为内部使用而创建的。
如果您打算将此用于公共用途,您将需要添加一些额外的数据验证以防止脚本注入。
这不是万无一失的,应该仅用于受信任的数据。
以下函数将输出如下内容:
$var = array(
'primarykey' => array(
'test' => array(
'var' => array(
1 => 99,
2 => 500,
),
),
'abc' => 'd',
),
);
Run Code Online (Sandbox Code Playgroud)
这是函数(注意:函数当前是为 oop 实现格式化的。)
public function outArray($array, $lvl=0){
$sub = $lvl+1;
$return = "";
if($lvl==null){
$return = "\t\$var = array(\n";
}
foreach($array as $key => $mixed){
$key = trim($key);
if(!is_array($mixed)){
$mixed = trim($mixed);
}
if(empty($key) && empty($mixed)){continue;}
if(!is_numeric($key) && !empty($key)){
if($key == "[]"){
$key = null;
} else {
$key = "'".addslashes($key)."'";
}
}
if($mixed === null){
$mixed = 'null';
} elseif($mixed === false){
$mixed = 'false';
} elseif($mixed === true){
$mixed = 'true';
} elseif($mixed === ""){
$mixed = "''";
}
//CONVERT STRINGS 'true', 'false' and 'null' TO true, false and null
//uncomment if needed
//elseif(!is_numeric($mixed) && !is_array($mixed) && !empty($mixed)){
// if($mixed != 'false' && $mixed != 'true' && $mixed != 'null'){
// $mixed = "'".addslashes($mixed)."'";
// }
//}
if(is_array($mixed)){
if($key !== null){
$return .= "\t".str_repeat("\t", $sub)."$key => array(\n";
$return .= $this->outArray($mixed, $sub);
$return .= "\t".str_repeat("\t", $sub)."),\n";
} else {
$return .= "\t".str_repeat("\t", $sub)."array(\n";
$return .= $this->outArray($mixed, $sub);
$return .= "\t".str_repeat("\t", $sub)."),\n";
}
} else {
if($key !== null){
$return .= "\t".str_repeat("\t", $sub)."$key => $mixed,\n";
} else {
$return .= "\t".str_repeat("\t", $sub).$mixed.",\n";
}
}
}
if($lvl==null){
$return .= "\t);\n";
}
return $return;
}
Run Code Online (Sandbox Code Playgroud)
或者,您可以使用我不久前也写过的这个脚本:
这个很适合复制和粘贴数组的一部分。
(使用序列化输出几乎不可能做到这一点)
不是最干净的功能,但它完成了工作。
这将输出如下:
$array['key']['key2'] = 'value';
$array['key']['key3'] = 'value2';
$array['x'] = 7;
$array['y']['z'] = 'abc';
Run Code Online (Sandbox Code Playgroud)
还要注意用户输入。这是代码。
public static function prArray($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 .= self::prArray($a, $path."['{$key}']", false);
}
}
}
if($top){
$return = "";
foreach(explode($delimiter, $data) as $value){
if(!empty($value)){
$return .= '$array'.$value."<br>";
}
};
echo $return;
}
return $data;
}
Run Code Online (Sandbox Code Playgroud)