php tri-csv-4.php 2020-02-28_17-44_test.csv > 2020-02-28_17-44_test-tri-3-11.csv
php en ligne de commande :
tri-csv-4.php
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;
}
?>
fichier de test :
2020-02-28_17-44_test.csv
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
résutat
2020-02-28_17-44_test-tri-3-11.csv
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;;;;;
Cela fonctionne
Si quelqu'un veut tester
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 ]));
Maintenant je vais tester avec mon serveur
Merci pour tout
php tri-csv-4.php 2020-02-28_17-44_test.csv > 2020-02-28_17-44_test-tri-3-11.csv
php en ligne de commande :
tri-csv-4.php
[code]
#!/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]
fichier de test :
2020-02-28_17-44_test.csv
[code]
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]
résutat
2020-02-28_17-44_test-tri-3-11.csv
[code]
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;;;;;
[/code]
Cela fonctionne
Si quelqu'un veut tester
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 ]));
Maintenant je vais tester avec mon serveur
Merci pour tout