这是一个php数组,我想通过带有特定标头的php将其转换为.txt文件(文本文件)。 请帮帮我!
Array:
$array = [
"fname" => "Jon",
"lname" => "Snow",
"class" => "Six",
"School" => "Abc"
];
标题:“名字”“姓氏”“班级”“学校”
.txt文件应如下所示:
FirstName LastName Class School
Jon Snow Six Abc
我尝试过以下一种方法,但这不适用于.txt文件
public static function ChangeArrayToTxt($result)
{
$basePath = dirname(\Yii::getAlias('@frontend')).'/var';
$filename="details.txt";
$file_path=$basePath.'/'.$filename;
if (!file_exists($basePath)) {
mkdir($basePath, 0777,true);
$output = fopen($file_path, "w");
chmod($file_path,0777);
fputcsv($output, array('FirstName', 'LastName', 'Class', 'School),"\t");
$upload['success']="FULFILLMENT CSV Uploaded";
fwrite($output, print_r($result,true));
}
}
答案 0 :(得分:0)
<?php
$txt1 = "";
$txt2 = "";
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$colors = array("red", "green", "blue", "yellow");
foreach ($colors as $key => $value) {
$txt1=$txt1."$key ";
}
echo " ";
foreach ($colors as $key => $value) {
$txt2 = $txt2."$value ";
}
$txt = $txt1."<\n>".$txt2;
fwrite($myfile, $txt);
fclose($myfile);
header("Location: /newfile.txt");
die();
?>