Huhu
geht net:
<?php
$fn = 'report.log';
$content = file($fn);
$haystack = array();
foreach($content as $c){
list ($begriff,$text) = explode(':',$c);
if ( $begriff != 'processor') continue;
$haystack[ trim( $begriff) ][] = trim( $text);
}
foreach ($haystack as $k => $v){
printf( 'Zum Begriff %s gibt es %d Zeilen<br>','processor',count($haystack['processor']) );
printf( 'Zum Begriff %s gibt es %d Zeilen<br>','pentium',count($haystack['pentium']) );
}
?>
was stimmt nicht ?
du bringst die Keys (Schlüssel) mit den Values (Werte) durcheinander.
Also nochmal von vorne ;-)
- list ($begriff,$text) = explode(':',$c);
diese Zeile zerlegt eine Zeile des TXT-Files am :
- if ( $begriff != 'processor') continue;
wenn $begriff != 'processor' dann ignorieren
muss an dieser Stelle noch getrimmt werden, da $begriff ja Blanks enthält.
- $haystack[ trim( $begriff) ][] = trim( $text);
hier wird $begriff als KEY für den Array-Eintrag genommen
in diesem Falle ist es ebenfalls ein Array, an dieses wird als neues Item $text angehängt.
- printf( 'Zum Begriff %s gibt es %d Zeilen<br>','pentium',count($haystack['pentium']) );
Soweit so einfach, jetzt möchtest Du alle Processortypen ausgeben.
Die stehen aber nicht als KEY sondern als VALUE im Array,
deshalb kann es so nicht funktionieren.
Die stehen als Array in $haystack['processor'].
Zum ausgeben probier einfach mal
echo join('<br>',$haystack['processor']);
da sind jetzt vermutlich noch allerhand doppelt Einträge drin.
Um diese zu zählen gibt es in PHP eine Funktion array_count_values, aber hier mal zu Fuss.
$Result = $haystack['processor'];
$occ = array();
foreach($Result as $r){
$occ[$r] ++;
}
Die Ausgabe erfolgt dann wie gehabt
foreach($occ as $k => $v){
printf( 'Vom Typ %s gibt es %d <br>',$k,$v);
}
so far...
lulu