Oui, tu peux aisément charger tout le fichier sous forme d'un tableau de tableaux avec par exemple une boucle while et fgetcsv. Ensuite tu peux récupérer la première ligne [0] ou array_shift pour la dépiler du tableau et tu peux filtrer les lignes avec par exemple array_filter.Cela vous parait facile d'utiliser php pour faire ce que je fais en bash?
Plus ou moins, quand je veux un outil à utiliser en ligne de commande ou quand c'est plus pratique que ou qu'il n'est pas utile de passer par le navigateur.Vous utilisez souvent en ligne de commande du php?
Anne#!/usr/bin/php
<?php
// init var
$delimiter = ';';
$data = [];
// load the file in $data
$fileRead = fopen('contacts.csv', 'r');
while ($line = fgetcsv($fileRead, 0, $delimiter))
$data[] = $line;
fclose($fileRead);
// extract hader
$header = array_shift($data);
// extract categorized lines
$categorized = array_filter($data, function ($line) {
return $line[36];
});
// extract uncategorized lines
$uncategorized = array_filter($data, function ($line) {
return !$line[36];
});
// show the results
// var_dump($header);
// var_dump($categorized);
// var_dump($uncategorized);
$col36values = array_column($categorized, 36);
$col0values = array_column($categorized, 0);
$col1values = array_column($categorized, 1);
$col2values = array_column($categorized, 2);
/* array_multisort(
$col36values, SORT_ASC,
$col2values, SORT_ASC,
$col0values, SORT_ASC,
$col1values, SORT_ASC,
$categorized);
marche pas
PHP Parse error: syntax error, unexpected ';', expecting ',' or ')' in /home/anne/sda1/anne/Documents/anne/linux/php/ligne-cde/tri-vcard.php on line 42
*/
array_multisort($col36values, SORT_ASC, $col2values, SORT_ASC, $col0values, SORT_ASC, $col1values, SORT_ASC, $categorized);
// Cela fonctionne . Pourquoi le array_multisort ne fonctionne pas quand il est sur plusieurs lignes
$col36values = array_column($uncategorized, 36);
$col0values = array_column($uncategorized, 0);
$col1values = array_column($uncategorized, 1);
$col2values = array_column($uncategorized, 2);
array_multisort($col36values, SORT_ASC, $col2values, SORT_ASC, $col0values, SORT_ASC, $col1values, SORT_ASC, $uncategorized);
// create the new file
$fileWrite = fopen('contacts-new-4.csv', 'w');
// push header
// fputcsv($fileWrite, $header, $delimiter);
fwrite($fileWrite, implode($delimiter, $header));
fwrite($fileWrite, "\n");
// push categorized lines
foreach($categorized as $line) {
fwrite($fileWrite, implode($delimiter, $line));
fwrite($fileWrite, "\n"); }
// push uncategorized lines
foreach($uncategorized as $line) {
fwrite($fileWrite, implode($delimiter, $line));
fwrite($fileWrite, "\n"); }
fclose($fileWrite);
?>
function listSort($array, $sort, $direction) {
$args = [];
for ($i = 0; $i < count($sort); $i++)
$args = array_merge($args, [
array_column($array, $sort[$i]),
$direction[$i]
]);
call_user_func_array('array_multisort', array_merge($args, [ &$array ]));
return $array;
}
// pour remplacer ça
$col36values = array_column($categorized, 36);
$col0values = array_column($categorized, 0);
$col1values = array_column($categorized, 1);
$col2values = array_column($categorized, 2);
array_multisort($col36values, SORT_ASC, $col2values, SORT_ASC, $col0values, SORT_ASC, $col1values, SORT_ASC, $categorized);
// par ça
$categorized = listSort(
$categorized,
[ 36, 0, 1, 2 ],
[ SORT_ASC, SORT_ASC, SORT_ASC, SORT_ASC ]);
function listSort($array, $sort, $direction) {
$args = [];
for ($i = 0; $i < count($sort); $i++)
$args[] = [
array_column($array, $sort[$i]),
$direction[$i]
];
$args[] = [ &$array ];
call_user_func_array('array_multisort', array_merge(...$args));
return $array;
}
function listSort($array, $sort, $direction) {
$args = [];
for ($i = 0; $i < count($sort); $i++) {
$args[] = array_column($array, $sort[$i]);
$args[] = $direction[$i];
}
$args[] = &$array;
array_multisort(...$args);
return $array;
}Code : Tout sélectionner
#!/usr/bin/php
<?php
$fichier = $argv[1];
// init var
$delimiter = ';';
$data = [];
// load the file in $data
//
$fileRead = fopen($fichier, 'r');
while ($line = fgetcsv($fileRead, 0, $delimiter))
$data[] = $line;
fclose($fileRead);
// extract header
$header = array_shift($data);
// extract categorized lines
$categorized = array_filter($data, function ($line) {
return $line[36];
});
// extract uncategorized lines
$uncategorized = array_filter($data, function ($line) {
return !$line[36];
});
echo "nbre categorized '".count($categorized)."'\n";
echo "nbre uncategorized '".count($uncategorized)."'\n";
// show the results
// var_dump($header);
// var_dump($categorized);
// var_dump($uncategorized);
/*
$categorized = listSort(
$categorized,
[ 36, 0, 1, 2 ],
[ SORT_ASC, SORT_DESC, SORT_ASC, SORT_ASC ]);
$uncategorized = listSort(
$uncategorized,
[ 36, 0, 1, 2 ],
[ SORT_ASC, SORT_DESC, SORT_ASC, SORT_ASC ]);
*/
$col = array( 36, 2, 0, 1 );
$sort = array( SORT_ASC, SORT_DESC, SORT_ASC, SORT_ASC );
var_dump($col);
var_dump($sort);
$prenom_tri = 1;
$nom_tri = 2;
$nom_complet_tri = 3;
$prenom_ord = SORT_DESC;
$nom_ord = SORT_ASC;
$nom_complet_ord = SORT_ASC;
$col[$prenom_tri] = $prenom_tri;
$col[$nom_tri] = $nom_tri;
$col[$nom_complet_tri] = $nom_complet_tri;
$sort[$prenom_tri] = $prenom_ord;
$sort[$nom_tri] = $nom_ord;
$sort[$nom_complet_tri] = $nom_complet_ord;
var_dump($col);
var_dump($sort);
$categorized = listSort(
$categorized, $col, $sort);
$uncategorized = listSort(
$uncategorized, $col, $sort);
// Maintenant on imprime chaque ligne
// l'entête
$ligne = implode($delimiter, $header);
print "".$ligne."\n";
// chaque ligne avec catégorie non vide
foreach($categorized as $line) {
$ligne = implode($delimiter, $line);
print "".$ligne."\n"; }
// push uncategorized lines
foreach($uncategorized as $line) {
$ligne = implode($delimiter, $line);
print "".$ligne."\n"; }
function listSort($array, $sort, $direction) {
$args = [];
for ($i = 0; $i < count($sort); $i++) {
$args[] = array_column($array, $sort[$i]);
$args[] = $direction[$i];
}
$args[] = &$array;
call_user_func_array('array_multisort', array_merge($args, [ &$array ]));
return $array;
}
?>
Code : Tout sélectionner
First Name;Last Name;Display Name;Nickname;E-mail Address;E-mail 2 Address;E-mail 3 Address;Home Phone;Business Phone;Mobile Phone;Home Fax;Business Fax;Pager;Home Street;Home Address 2;Home City;Home State;Home Postal Code;Home Country;Business Address;Business Address 2;Business City;Business State;Business Postal Code;Business Country;Country Code;Related name;Job Title;Department;Organization;Notes;Birthday;Anniversary;Gender;Web Page;Web Page 2;Categories
Aprenom1 Anom1;;Aprenom1 Anom1;;[email protected];[email protected];;+33 1 23 45 67 89;+33 2 87 65 43 21;+33 6 12 34 56 78;+33 9 12 34 56 78;+33 9 87 65 43 21;;rue1;rue2;meylan;rhone alpes;38240;france;rue1T;rue2T;Grenoble;isere;38000;france;;;;;;notes;1604-12-08 00:00:00;;;;;
Aprenom2;Anom2;Aprenom2 Anom2;;[email protected];[email protected];;+33 1 23 45 67 89;+33 9 12 34 76 78;+33 6 56 78 12 34;;;;rue D;rue D étendue;ville D;paca D;12345;france;rue T;rue T étendue;ville T;paca T;23456;france;;;;;;note lig 1 note lig 2 note lig 3;2020-01-05 00:00:00;;;www.xxx.com;;test
Aprenom3;Anom3;Aprenom3 Anom3;;;;;;;+33 6 12 34 56 78;;;;;;;;;;;;;;;;;;;;;;;;;;;test
Aprenom5;Anom5;Aprenom5 Anom5;;;;;;+33 9 87 65 43 21;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Aprenom4;Anom4;Aprenom4 Anom4;;;;;+33 1 23 45 67 89;+33 9 12 34 56 78;+33 6 87 65 43 21;;;;;;;;;;;;;;;;;;;;;;;;;;;test
Aprenom7;Anom7;Aprenom7 Anom7;;;;;+33 1 23 45 67 89;;;;;;;;;;;;;;;;;;;;;;;;;;;;;test
;Aprenom Anom;Aprenom Anom;;[email protected];[email protected];[email protected];+33 9 12 34 56 78;+33 9 87 65 43 21;+33 6 12 56 34 78;+33 8 12 34 56 78;+33 8 87 65 43 21;;rueD;;VILLED;;01234;;rueT;;VilleT;;34567;;;;;;;notes en tout genre AAAAAA aaaaaa;2019-12-18 00:00:00;;;;;
;;Aprenom6 Anom6;;;;;+33 1 23 45 67 89;;;;;;;;;;;;;;;;;;;;;;;;;;;;;test
Code : Tout sélectionner
nbre categorized '5'
nbre uncategorized '3'
array(4) {
[0]=>
int(36)
[1]=>
int(2)
[2]=>
int(0)
[3]=>
int(1)
}
array(4) {
[0]=>
int(4)
[1]=>
int(3)
[2]=>
int(4)
[3]=>
int(4)
}
array(4) {
[0]=>
int(36)
[1]=>
int(1)
[2]=>
int(2)
[3]=>
int(3)
}
array(4) {
[0]=>
int(4)
[1]=>
int(3)
[2]=>
int(4)
[3]=>
int(4)
}
First Name;Last Name;Display Name;Nickname;E-mail Address;E-mail 2 Address;E-mail 3 Address;Home Phone;Business Phone;Mobile Phone;Home Fax;Business Fax;Pager;Home Street;Home Address 2;Home City;Home State;Home Postal Code;Home Country;Business Address;Business Address 2;Business City;Business State;Business Postal Code;Business Country;Country Code;Related name;Job Title;Department;Organization;Notes;Birthday;Anniversary;Gender;Web Page;Web Page 2;Categories
Aprenom7;Anom7;Aprenom7 Anom7;;;;;+33 1 23 45 67 89;;;;;;;;;;;;;;;;;;;;;;;;;;;;;test
Aprenom4;Anom4;Aprenom4 Anom4;;;;;+33 1 23 45 67 89;+33 9 12 34 56 78;+33 6 87 65 43 21;;;;;;;;;;;;;;;;;;;;;;;;;;;test
Aprenom3;Anom3;Aprenom3 Anom3;;;;;;;+33 6 12 34 56 78;;;;;;;;;;;;;;;;;;;;;;;;;;;test
Aprenom2;Anom2;Aprenom2 Anom2;;[email protected];[email protected];;+33 1 23 45 67 89;+33 9 12 34 76 78;+33 6 56 78 12 34;;;;rue D;rue D étendue;ville D;paca D;12345;france;rue T;rue T étendue;ville T;paca T;23456;france;;;;;;note lig 1 note lig 2 note lig 3;2020-01-05 00:00:00;;;www.xxx.com;;test
;;Aprenom6 Anom6;;;;;+33 1 23 45 67 89;;;;;;;;;;;;;;;;;;;;;;;;;;;;;test
;Aprenom Anom;Aprenom Anom;;[email protected];[email protected];[email protected];+33 9 12 34 56 78;+33 9 87 65 43 21;+33 6 12 56 34 78;+33 8 12 34 56 78;+33 8 87 65 43 21;;rueD;;VILLED;;01234;;rueT;;VilleT;;34567;;;;;;;notes en tout genre AAAAAA aaaaaa;2019-12-18 00:00:00;;;;;
Aprenom5;Anom5;Aprenom5 Anom5;;;;;;+33 9 87 65 43 21;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Aprenom1 Anom1;;Aprenom1 Anom1;;[email protected];[email protected];;+33 1 23 45 67 89;+33 2 87 65 43 21;+33 6 12 34 56 78;+33 9 12 34 56 78;+33 9 87 65 43 21;;rue1;rue2;meylan;rhone alpes;38240;france;rue1T;rue2T;Grenoble;isere;38000;france;;;;;;notes;1604-12-08 00:00:00;;;;;
En fait t'as fait un mix des différentes fonctions et c'est pas bon comme ça. Coup de chance que tu n'aies pas d'erreur et que ça fonctionne. Il faut prendre la dernière si tu es en php 5.6+J'ai dû mal à comprendre comment cela se passe avec ces 2 lignes
$args[] = &$array;
call_user_func_array('array_multisort', array_merge($args, [ &$array ]));
function listSort($array, $sort, $direction)
{
$args = array();
for ($i = 0; $i < count($sort); $i++) {
$args[] = array_column($array, $sort[$i]);
$args[] = $direction[$i];
}
$args[] = &$array;
call_user_func_array('array_multisort', $args);
return $array;
}Code : Tout sélectionner
$nombre = array (1, 2, 3);
//$ordre = array (SORT_ASC, SORT_DESC); // 1
$ordre = array ("SORT_ASC", "SORT_DESC"); // 2
//$ordre = array ('SORT_ASC', 'SORT_DESC'); // 3
Code : Tout sélectionner
Tri Prénom :
<select name = "$_prenom_tri" value="'.$prenom_tri.'" >';
foreach($nombre as $select_nbre){
echo '<option value = "'.$select_nbre.'" > '.$select_nbre.' </option>';
}
echo '</select>
ordre
<select name = "$_prenom_ord" value="'.$prenom_ord.'" >';
foreach($ordre as $select_ordre){
echo '<option value = "'.$select_ordre.'" > '.$select_ordre.' </option>';
}
echo '</select> <br><br>
// utiliser un tableau associatif
$orderOptions = [
SORT_ASC => 'SORT_ASC',
SORT_DESC => 'SORT_DESC'
];
<select name="order">
<?php foreach ($orderOptions as $value => $label): ?>
<option value="<?php echo $value; ?>"><?php echo $label; ?></option>
<?php endforeach; ?>
</select>
if (array_key_exists($_POST['order'], $orderOptions))
$orderOptions = [
SORT_ASC => 'Ascendant',
SORT_DESC => 'Déscendant'
];