写入文件时数组未排序
Array isn't sorting when writing to file
我写了这个脚本:
<?PHP
$file_handle = fopen("info.txt", "rb");
while (!feof($file_handle) ) {
$line_of_text = fgets($file_handle);
$parts[] = explode('|', $line_of_text);
}
fclose($file_handle);
$a = $parts;
function cmp($a,$b){
return strtotime($a[8])<strtotime($b[8])?1:-1;
};
uasort($a, 'cmp');
$failas = "dinfo.txt";
$fh = fopen($failas, 'w');
for($i=0; $i<count($a); $i++){
$txt=implode('|', $a[$i]);
fwrite($fh, $txt);
}
fclose($fh);
?>
当我使用时:
print_r($a);
在
之后
uasort($a, 'cmp');
然后我可以看到排序的数组。但是当我使用这些命令写入文件时:
$fh=fopen($failas, 'w');
for($i=0; $i<count($a); $i++){
$txt=implode('|', $a[$i]);
fwrite($fh, $txt);
}
fclose($fh);
它显示未排序的信息,我做错了什么?
这应该适合你:
在这里,我首先将您的文件放入一个 file()
的数组中,其中每一行都是一个数组元素。我忽略了每行末尾的空行和换行符。
在此之后,我用 usort()
. Where I first get all dates and times from each line by explode()
'ing it. After this I simply get the timestamp of each date with strtotime()
对数组进行排序并相互比较。
最后我只是用 file_put_contents()
, where I also add a new line character at the end of each line with array_map()
.
保存文件
<?php
$lines = file("test.txt", FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES);
usort($lines, function($a, $b){
list($aDate, $aTime) = explode(" ", explode("|", $a)[substr_count($a, "|")]);
list($bDate, $bTime) = explode(" ", explode("|", $b)[substr_count($b, "|")]);
if(strtotime("$aDate $aTime") == strtotime("$bDate $bTime"))
return 0;
return strtotime("$aDate $aTime") < strtotime("$bDate $bTime") ? 1 : -1;
});
file_put_contents("test.txt", array_map(function($v){return $v . PHP_EOL;}, $lines));
?>
旁注:
我建议您将此数据保存在数据库中,这样可以更灵活地排序和获取数据!
编辑:
对于 php 版本 (echo phpversion();
) <5.3 的用户,只需将匿名函数更改为普通函数并将函数名称作为字符串传递,如下所示:
<?php
$lines = file("test.txt", FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES);
function timestampCmp($a, $b) {
$aExploded = explode("|", $a);
$bExploded = explode("|", $b);
list($aDate, $aTime) = explode(" ", $aExploded[substr_count($a, "|")]);
list($bDate, $bTime) = explode(" ", $bExploded[substr_count($b, "|")]);
if(strtotime("$aDate $aTime") == strtotime("$bDate $bTime"))
return 0;
return strtotime("$aDate $aTime") < strtotime("$bDate $bTime") ? 1 : -1;
}
function addEndLine($v) {
return $v . PHP_EOL;
}
usort($lines, "timestampCmp");
file_put_contents("test.txt", array_map("addEndLine", $lines));
?>
我写了这个脚本:
<?PHP
$file_handle = fopen("info.txt", "rb");
while (!feof($file_handle) ) {
$line_of_text = fgets($file_handle);
$parts[] = explode('|', $line_of_text);
}
fclose($file_handle);
$a = $parts;
function cmp($a,$b){
return strtotime($a[8])<strtotime($b[8])?1:-1;
};
uasort($a, 'cmp');
$failas = "dinfo.txt";
$fh = fopen($failas, 'w');
for($i=0; $i<count($a); $i++){
$txt=implode('|', $a[$i]);
fwrite($fh, $txt);
}
fclose($fh);
?>
当我使用时:
print_r($a);
在
之后uasort($a, 'cmp');
然后我可以看到排序的数组。但是当我使用这些命令写入文件时:
$fh=fopen($failas, 'w');
for($i=0; $i<count($a); $i++){
$txt=implode('|', $a[$i]);
fwrite($fh, $txt);
}
fclose($fh);
它显示未排序的信息,我做错了什么?
这应该适合你:
在这里,我首先将您的文件放入一个 file()
的数组中,其中每一行都是一个数组元素。我忽略了每行末尾的空行和换行符。
在此之后,我用 usort()
. Where I first get all dates and times from each line by explode()
'ing it. After this I simply get the timestamp of each date with strtotime()
对数组进行排序并相互比较。
最后我只是用 file_put_contents()
, where I also add a new line character at the end of each line with array_map()
.
<?php
$lines = file("test.txt", FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES);
usort($lines, function($a, $b){
list($aDate, $aTime) = explode(" ", explode("|", $a)[substr_count($a, "|")]);
list($bDate, $bTime) = explode(" ", explode("|", $b)[substr_count($b, "|")]);
if(strtotime("$aDate $aTime") == strtotime("$bDate $bTime"))
return 0;
return strtotime("$aDate $aTime") < strtotime("$bDate $bTime") ? 1 : -1;
});
file_put_contents("test.txt", array_map(function($v){return $v . PHP_EOL;}, $lines));
?>
旁注:
我建议您将此数据保存在数据库中,这样可以更灵活地排序和获取数据!
编辑:
对于 php 版本 (echo phpversion();
) <5.3 的用户,只需将匿名函数更改为普通函数并将函数名称作为字符串传递,如下所示:
<?php
$lines = file("test.txt", FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES);
function timestampCmp($a, $b) {
$aExploded = explode("|", $a);
$bExploded = explode("|", $b);
list($aDate, $aTime) = explode(" ", $aExploded[substr_count($a, "|")]);
list($bDate, $bTime) = explode(" ", $bExploded[substr_count($b, "|")]);
if(strtotime("$aDate $aTime") == strtotime("$bDate $bTime"))
return 0;
return strtotime("$aDate $aTime") < strtotime("$bDate $bTime") ? 1 : -1;
}
function addEndLine($v) {
return $v . PHP_EOL;
}
usort($lines, "timestampCmp");
file_put_contents("test.txt", array_map("addEndLine", $lines));
?>