Subversion Repositories Applications.framework

Compare Revisions

Ignore whitespace Rev 380 → Rev 381

/trunk/framework/utilitaires/Tableau.php
12,6 → 12,7
* @version $Id$
* @link /doc/framework/
*/
// TODO : réaliser une seule méthode pour trierMD et trierMDType
class Tableau {
/**
* Etend le tableau à étendre avec les données du tableau à copier. Si des clés sont identiques entre les deux tableaux
60,7 → 61,7
public static function trierMD($array, $cols) {
$colarr = array();
foreach ($cols as $col => $order) {
$colarr[$col] = array();
$colarr[$col] = array();
foreach ($array as $k => $row) {
$colarr[$col]['_'.$k] = strtolower($row[$col]);
}
68,7 → 69,7
$params = array();
foreach ($cols as $col => $order) {
$params[] =& $colarr[$col];
$params = array_merge($params, (array)$order);
$params = array_merge($params, (array) $order);
}
call_user_func_array('array_multisort', $params);
$ret = array();
89,5 → 90,49
}
return $ret;
}
/**
* Méthode réalisant un tri d'un tableau multidimension.
* A utiliser de cette façon:
* EfloreTriage::trieMultiple( $tableau_multidimension, array('ma_cle1', 'ma_cle1_ordre_tri', 'ma_cle2', 'ma_cle2_ordre_tri'), $type_de_tri);
* Utiliser les constantes php SORT_DESC ou SORT_ASC pour l'odre de tri.
* Pour le type de tri : utiliser 'nat' pour un trie naturel, 'cs' pour tri sensible à la casse ou 'ci' pour insensible.
* @param array le tableau à trier
* @param array le talbeau des colonnes à trier constituer d'un suite de nom de clé de tableau et d'ordre de tri.
* @return array le tableau trié.
*/
public static function trierMDType(&$tableau, $cols, $type = 'ci') {
self::$tri_multi_dimension = $cols;
self::$tri_type = $type;
usort($tableau, array('self', 'comparer'));
return $tableau;
}
private static function comparer($a, $b) {
$cols = self::$tri_multi_dimension;
$i = 0;
$resultat = 0;
$colonne_nbre = count($cols);
while ($resultat == 0 && $i < $colonne_nbre) {
$mot_01 = self::enleverAccents($b[$cols[$i]]);
$mot_02 = self::enleverAccents($a[$cols[$i]]);
switch (self::$tri_type) {
case 'nat' :
$resultat = ($cols[$i + 1] == SORT_DESC) ? strnatcmp($mot_01, $mot_02) : strnatcmp($mot_02, $mot_01);
break;
case 'cs' :
$resultat = ($cols[$i + 1] == SORT_DESC) ? strcmp($mot_01, $mot_02) : strcmp($mot_02, $mot_01);
break;
case 'ci' :
$resultat = ($cols[$i + 1] == SORT_DESC) ? strcasecmp($mot_01, $mot_02) : strcasecmp($mot_02, $mot_01);
break;
default:
$resultat = ($cols[$i + 1] == SORT_DESC) ? strcasecmp($mot_01, $mot_02) : strcasecmp($mot_02, $mot_01);
break;
}
$i += 2;
}
return $resultat;
}
}
?>