$fic = data.txt; $taille_fic = filesize("$fic"); // si le fichier existe et qu'il n'est pas vide if (file_exists("$fic") && ($taille_fic != 0)) { $tab_fic = file("$fic"); $nb=count($tab_fic); $i = 0; while ($i < $nb) // Tant que l'on est pas à la fin du fichier { $tab_1 = explode('|', $tab_fic[$i]); // on explose la ligne à l'indice i $tab_2 = explode('|', $tab_fic[$i+1]); // on explose la ligne à i+1 $nom = $tab_1[0]; $prenom = $tab_1[1]; $tps = $tab_1[2] $tab = array(); if (isset($tab[$nom][$prenom]) // si le couple colonne - ligne existe { $tab[$nom][$prenom] = $tab[$nom][$prenom] + $tps; } else { $tab[$nom][$prenom] = $tab[$nom][$prenom] // sinon on crée le couple colonne - ligne }
<?php $fic = "data.txt"; $taille_fic = filesize($fic); // si le fichier existe et qu'il n'est pas vide if (file_exists($fic) && ($taille_fic != 0)) { $tab_fic = file($fic); print_r($tab_fic); echo ' '; $nb=count($tab_fic); echo $nb.' '; $i = 0; $t=array(); foreach($tab_fic as $k => $v){ //explode('|',$v,3); echo $v; $t[] = explode ("|",$v); } echo ' '; echo ''; print_r($t); echo ' '; } // utilise sort(); //http://php.net/manual/en/function.sort.php ?>
$sContents = file($fic); $tab = array(); foreach ($sContents as $line) { $aRes = str_getcsv($line, '|'); if (false === array_key_exists($aRes[0], $tab)) $tab[$aRes[0]] = array(); array_push($tab[$aRes[0]], $aRes[2]); } echo ''; print_r($tab); echo ' ';