问题
I have an associative array in php, for example with the values:
"apple" => "green"
"banana" => "yellow"
"grape" => "red"
My question is, how can I write the keys and values for this array to a .txt
file into two perfect columns?
By which I mean into two columns with a uniform distance between them all the way down
回答1:
You can use str_pad() php function for the output. http://php.net/manual/en/function.str-pad.php
Code:
<?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);
Output:
apple green
banana yellow
grape red
// Getting the length dynamically version.
<?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);
回答2:
Maybe a long shot, but you could possible do something like find the max array key length and then use that as a guide for how many spaces you want in between the words.
eg.
You could get the max array key length using strlen()
like this:
$maxLength = 0;
foreach($array as $key => $item){
if(strlen($key) > $maxLength){
$maxLength = strlen($key);
}
}
$maxLength += 5; //or any spacing value here
And then use str_pad()
to add padding to the word like this:
$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);
This probably isn't the best solution but you could probably make it more efficient.
回答3:
Update
I revised the functionality, and the below will work with a string (array key) of any length.
$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;
Example
I don't understand why you'd want to do something like this, but this will do as you desire:
$str = '';
foreach($array as $key => $item){
$str .= $key . "\t" .$item ."\n";
}
file_put_contents('path/to/file', $str);
Example
You'd obviously have to test the file_put_contents()
to ensure it succeeded, but I'll leave that to you.
You'd just have to change the ammount of tabs (\t
) if you run into any long strings. In your case, it'd probably be best if you went with 2 (\t\t
).
回答4:
$Offer
is your array
$file = 'people.txt';
$content = '';
foreach ($Offer as $key => $value) {
$content .= $key.'='.$value;
// Write the contents back to the file
file_put_contents($file, $current);
}
来源:https://stackoverflow.com/questions/27833521/writing-to-txt-file-in-columns-with-php