Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用php写入列中的.txt文件_Php_Arrays_File - Fatal编程技术网

使用php写入列中的.txt文件

使用php写入列中的.txt文件,php,arrays,file,Php,Arrays,File,我在php中有一个关联数组,例如值: "apple" => "green" "banana" => "yellow" "grape" => "red" 我的问题是,如何将此数组的键和值写入.txt文件,并将其写入两个完美的列中? 我的意思是分成两列,两列之间的距离一致 我修改了功能,下面的内容将使用任意长度的字符串(数组键) $longest = 0; // find the longest string. foreach ($array as $key => $val

我在php中有一个关联数组,例如值:

"apple" => "green"
"banana" => "yellow"
"grape" => "red"
我的问题是,如何将此数组的键和值写入
.txt
文件,并将其写入两个完美的列中? 我的意思是分成两列,两列之间的距离一致 我修改了功能,下面的内容将使用任意长度的字符串(数组键)

$longest = 0;
// find the longest string.
foreach ($array as $key => $val) {
    $c = strlen($key);
    $longest = ($c > $longest) ? $c : $longest;
}

$distance = 5;
$str = '';
// now loop through and apply the appropriate space
foreach ($array as $key => $val) {
    $c = strlen($key);
    $space = $distance + ($longest - $c);
    $str .= $key . str_repeat(" ", $space) . $val . "\n";
}

echo $str;


我不明白你为什么想做这样的事,但这会满足你的愿望:

$str = '';
foreach($array as $key => $item){
  $str .= $key . "\t" .$item ."\n";
}
file_put_contents('path/to/file', $str);


显然,您必须测试
文件\u put\u contents()
以确保它成功,但我将留给您


如果遇到任何长字符串,只需更改选项卡的数量(
\t
)。在您的情况下,最好使用2(
\t\t
)。

$Offer
是您的
数组

$file = 'people.txt';
$content = '';

foreach ($Offer as $key => $value) { 
   $content .= $key.'='.$value;
   // Write the contents back to the file
   file_put_contents($file, $current);
}

可能需要很长的时间,但是您可以做一些事情,比如找到最大数组键长度,然后使用它作为指导,确定单词之间需要多少空格

例如

您可以使用
strlen()
获得最大数组键长度,如下所示:

$maxLength = 0;
foreach($array as $key => $item){
  if(strlen($key) > $maxLength){
    $maxLength = strlen($key);
  }
}
$maxLength += 5; //or any spacing value here
$str = '';
foreach($array as $key => $item){
  $str .= str_pad($key, $maxLength, ' ', STR_PAD_RIGHT) . $item . '\n'; //add padding to the right hand side of the word with spaces
}

file_put_contents('path/to/file', $str);
然后使用
str_pad()
为单词添加填充,如下所示:

$maxLength = 0;
foreach($array as $key => $item){
  if(strlen($key) > $maxLength){
    $maxLength = strlen($key);
  }
}
$maxLength += 5; //or any spacing value here
$str = '';
foreach($array as $key => $item){
  $str .= str_pad($key, $maxLength, ' ', STR_PAD_RIGHT) . $item . '\n'; //add padding to the right hand side of the word with spaces
}

file_put_contents('path/to/file', $str);

这可能不是最好的解决方案,但您可以提高效率。

您可以使用str\u pad()php函数进行输出。

代码:

<?php
$fruits = array( "apple" => "green",
                "banana" => "yellow",
                "grape" => "red" );

$filename = "file.txt";
$text = "";
foreach($fruits as $key => $fruit) {
    $text .= str_pad($key, 20)."  ".str_pad($fruit, 10 )."\n"; // Use str_pad() for uniform distance
}
$fh = fopen($filename, "w") or die("Could not open log file.");
fwrite($fh, $text) or die("Could not write file!");
fclose($fh);
//动态获取长度版本。

<?php
$fruits = array( "apple" => "green",
                "banana" => "yellow",
                "grape" => "red" );

$filename = "file.txt";

$maxKeyLength = 0;
$maxValueLength = 0;

foreach ($fruits as $key => $value) {
    $maxKeyLength = $maxKeyLength < strlen( $key ) ? strlen( $key ) : $maxKeyLength;
    $maxValueLength = $maxValueLength < strlen($value) ? strlen($value) : $maxValueLength ;
}

$text = "";
foreach($fruits as $key => $fruit) {
    $text .= str_pad($key, $maxKeyLength)."         ".str_pad($fruit, $maxValueLength )."\n"; //User str_pad() for uniform distance
}
$fh = fopen($filename, "w") or die("Could not open log file.");
fwrite($fh, $text) or die("Could not write file!");
fclose($fh);

为什么要这样做?您所指的文件类型是txt文件还是网页?@NullPoièteè格式化目的如果您想将键和值放入文本文件中??阅读有关中文件处理的更多信息php@Lex,是的,用于格式化,但用于哪种介质?又是哪种类型的文件?因为没有文件类型,你的问题很模糊。如果说第一个单词中有一个很长,那么标签就不起作用,是吗?@thePav Check edit,OP必须指定并提供更多信息:)但这正是我所想的。(用另一种方法来计算这些废话是毫无益处的)@Darren-这是我的问题。这些词的长度不同。因此,它们之间的空间会有所不同。Usin\t或\t\t不起作用,因为它不能在列之间保持一致的距离。但对于长字符串不起作用,@Lex我有一个适合长度较大的单词的解决方案。@SuryaElite,如果单词长度超过20个字符会怎么样?@Darren-我可以更改值。你的似乎是对的,但由于某些原因不起作用。@Lex,看看这个:它适合所有长度的单词。@thePav我刚刚告诉他str_pad()的功能。动态获取长度是个好主意。