Einen zweiten Thread zu öffnen ist in diesem Fall völlig unnötig, ich gehe einfach mal davon aus, dass dein Array noch garnicht richtig sortiert ist - oder hast du es zwischendurch schon einmal ausgegeben?
Ein array sortiert in eine Datei schreiben - wie?
Einklappen
X
-
Die Regeln | rtfm | register_globals | strings | SQL-Injections | [COLOR=silver][[/COLOR][COLOR=royalblue]–[/COLOR][COLOR=silver]][/COLOR]
-
So
PHP-Code:<?php
date_default_timezone_set('Europe/Berlin');
$lines = file_get_contents('/var/www/web22/log/test.txt');
foreach (explode("\n", $lines) as $line)
{
$result[] = explode('|', $line);
}
$sortMe = $result;
$sortCol = array();
foreach ($sortMe as $key => $row)
#while(list($key,$row)= each($sortMe));
{
$sortCol[$key] = strtotime(implode('-',array_reverse(explode('.',$row[3]))));
}
$result[] = array_multisort($sortCol, SORT_DESC, $sortMe);
$path = "/var/www/web22/log/";
$file = "fertig.txt";
$size = count($result);
if(file_exists($path.$file) && is_writeable($path.$file))
{
$fp = fopen($path.$file, 'w');
foreach( $result as $i=>$value )
# for ($i = 0; $i < $size; $i++)
{
$size1= count($result[$i]);
if(isset($result[$i][0]) && isset($result[$i][1]) && isset($result[$i][2])
&& isset($result[$i][3]) && isset($result[$i][4]) && ($result != ""))
{
$zeilen = "<a href=\"".$result[$i][0]."\">".$result[$i][1]."</a><b>"
.$result[$i][4]."</b>".[".$result[$i][3]."] ".$result[$i][2]."<br>";
fputs($fp, "$zeilen \n");
}
}
fclose($fp);
}else{
echo "Datei ".$path.$file." ist nicht beschreibbar oder exisistiert nicht!";
}
echo "<pre>";
print_r( $sortMe );
echo "</pre>";
?>Zuletzt geändert von janein; 06.04.2008, 14:05.
Kommentar
-
Das steht in der Datei:
o|b|c|07.04.08|1
t|w|c|09.04.08|0
u|b|z|10.04.08|0
pp|bz|cu|02.04.08|1
PHP-Code:Array
(
[0] => Array
(
[0] => u
[1] => b
[2] => z
[3] => 10.04.08
[4] => 0
)
[1] => Array
(
[0] => t
[1] => w
[2] => c
[3] => 09.04.08
[4] => 0
)
[2] => Array
(
[0] => o
[1] => b
[2] => c
[3] => 07.04.08
[4] => 1
)
[3] => Array
(
[0] => pp
[1] => bz
[2] => cu
[3] => 02.04.08
[4] => 1
)
[4] => Array
(
[0] =>
)
)
Danke, danke für Eure Vorschläge!
Nur dies offset Meldung stört ...Zuletzt geändert von janein; 06.04.2008, 16:17.
Kommentar
-
Datei in array einlesen - immer ein Key mehr - warum?
Das steht in der Datei:
o|b|c|07.04.08|1
t|w|c|09.04.08|0
u|b|z|10.04.08|0
pp|bz|cu|02.04.08|1
[PHP]
$lines = file_get_contents('test.txt');
foreach (explode("\n", $lines) as $line)
{
$result[] = explode('|', $line);
}
[⁄PHP]
So sieht das eingelesene array aus:
PHP-Code:Array
(
[0] => Array
(
[0] => u
[1] => b
[2] => z
[3] => 10.04.08
[4] => 0
)
[1] => Array
(
[0] => t
[1] => w
[2] => c
[3] => 09.04.08
[4] => 0
)
[2] => Array
(
[0] => o
[1] => b
[2] => c
[3] => 07.04.08
[4] => 1
)
[3] => Array
(
[0] => pp
[1] => bz
[2] => cu
[3] => 02.04.08
[4] => 1
)
[4] => Array
(
[0] =>
)
)
array [4] ist leer, aber wieso ist es da und wie werde ich es los, es werden doch nur 4 Zeilen eingelesen, oder nicht?Zuletzt geändert von janein; 06.04.2008, 16:27.
Kommentar
-
Original geschrieben von asp2php
ganz einfach, wenn du beim schreiben immer \n am Ende dran hängst und beim explode \n als Trenner verwendest, wundert dich das?
Wie macht man es besser, eine Datei Zeile für Zeile einzulesen - einfach ein anderes Zeichen wie #⁄n verwenden und nach # trennen?
Kommentar
-
Ok, das leuchtet ein :-)Code:Foo\n 1. Zeile hinzufügen Bar\n 2. Zeile hinzufügen Blubb\n 3. Zeile hinzufügen
Code:[COLOR=silver][[/COLOR][COLOR=royalblue]Foo[/COLOR][COLOR=silver]][/COLOR][COLOR=blue]\n[/COLOR][COLOR=silver][[/COLOR][COLOR=royalblue]Bar[/COLOR][COLOR=silver]][/COLOR][COLOR=blue]\n[/COLOR][COLOR=silver][[/COLOR][COLOR=royalblue]Blubb[/COLOR][COLOR=silver]][/COLOR][COLOR=blue]\n[/COLOR][COLOR=silver][[/COLOR][COLOR=silver]][/COLOR]
PHP-Code:if(!empty($line)) {
$result[] = explode('|', $line);
}
Die Regeln | rtfm | register_globals | strings | SQL-Injections | [COLOR=silver][[/COLOR][COLOR=royalblue]–[/COLOR][COLOR=silver]][/COLOR]
Kommentar
-
Original geschrieben von tontechniker
... Die einfachste Möglichkeit das zu verhindern ist beim Einlesen zu prüfen ob der aktuelle Teil leer ist:PHP-Code:if(!empty($line)) {
$result[] = explode('|', $line);
}
PHP-Code:$lines = file ('test2.txt');
#print_r($lines);
foreach ($lines as $key => $line)
{
$result[] = explode('|', $lines[$key]);
}
Was ist der Unterschied zu
PHP-Code:if(isset($line)) {
$result[] = explode('|', $line);
}
Zuletzt geändert von janein; 06.04.2008, 17:31.
Kommentar
-
Zb. dass "" bei !empty() false wird und bei isset() true ist.
isset() prüft eigentlich nur, ob die Variable vorhanden ist und nicht gleich null ist.
Die ganzen Unterschiede kannst du dir hier anschauen:
http://de.php.net/manual/de/types.comparisons.phpZuletzt geändert von TheFish511; 06.04.2008, 17:56.Assembler ist eine Methode,
Programme, die zu langsam laufen,
so umzuschreiben,
dass sie überhaupt nicht mehr laufen.
Kommentar
-
PHP-Code:function sortArray($a,$b){
$timeA = explode('.',$a[3]);
$timeB = explode('.',$b[3]);
$timeA = mktime(0,0,0,$timeA[1],$timeA[0],$timeA[2]);
$timeB = mktime(0,0,0,$timeB[1],$timeB[0],$timeB[2]);
if($timeA == $timeB){
return 0;
}
return ($timeA<$timeB)?-1:1;
}
$str[0] = 'o|b|c|07.04.08|1';
$str[1] = 't|w|c|09.04.08|0';
$str[2] = 'u|b|z|10.04.08|0';
$str[3] = 'pp|bz|cu|02.04.08|1';
$arr = array();
foreach($str as $wert){
$arr[] = explode('|',$wert);
}
usort($arr,"sortArray");
echo '<pre>';
var_dump($arr);
echo '</pre>';
Gruss
tobiGutes Tutorial | PHP Manual | MySql Manual | PHP FAQ | Apache | Suchfunktion für eigene Seiten
[color=red]"An error does not become truth by reason of multiplied propagation, nor does truth become error because nobody sees it."[/color]
Mohandas Karamchand Gandhi (Mahatma Gandhi) (Source)
Kommentar
Kommentar