Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentesRévision précédente
Prochaine révision
Révision précédente
kb_it:php [2022/10/26 17:43] – [Les tableaux] befekb_it:php [2025/02/13 15:59] (Version actuelle) befe
Ligne 22: Ligne 22:
     return array_keys($array) !== range(0, count($array) - 1);     return array_keys($array) !== range(0, count($array) - 1);
 } }
 +</code>
 +
 +=== Rechercher dans un tableau associatif ===
 +
 +<code php>
 +$people = array(
 +  2 => array(
 +    'name' => 'John',
 +    'fav_color' => 'green'
 +  ),
 +  5=> array(
 +    'name' => 'Samuel',
 +    'fav_color' => 'blue'
 +  )
 +);
 +$found_key = array_search('blue', array_column($people, 'fav_color'));
 +</code>
 +
 +Attention, ça renvoie 1 et non 5 (c'est le 2ème élément du tableau).
 +
 +Pour un gros tableau, il faut optimiser :
 +<code php>
 +$colors = array_column($people, 'fav_color');
 +$found_key = array_search('blue', $colors);
 </code> </code>
  
Ligne 33: Ligne 57:
     return $keys[$el];     return $keys[$el];
 }, array_keys($array)), array_values($array)); }, array_keys($array)), array_values($array));
 +</code>
 +
 +=== Tri multi-critères ===
 +
 +<code php>
 +usort ($rows, function ($a, $b) {
 +    $cmpAll = 0;
 +
 +    // code_etape . version
 +    $cmp_code_etape_version = strcmp($a->code_etape . $a->version, $b->code_etape . $b->version);
 +    $cmpAll += $cmp_code_etape_version == 0 ? 0 : ($cmp_code_etape_version > 1 ? 1 : -1) * 1000000;
 +
 +    // nature BLCC
 +    $cmpAll += (($b->nature == 'BLCC' ? 1 : 0) - ($a->nature == 'BLCC' ? 1 : 0)) * 100000;
 +
 +    // nature BLCA
 +    $cmpAll += (($b->nature == 'BLCA' ? 1 : 0) - ($a->nature == 'BLCA' ? 1 : 0)) * 10000;
 +
 +    // nature SEM
 +    $cmpAll += (($b->nature == 'SEM' ? 1 : 0) - ($a->nature == 'SEM' ? 1 : 0)) * 1000;
 +
 +    // code_UE_mere
 +    $cmp_code_UE_mere = strcmp($a->code_UE_mere, $b->code_UE_mere);
 +    $cmpAll += ($cmp_code_UE_mere == 0 ? 0 : ($cmp_code_UE_mere > 0 ? 1 : -1)) * 10;
 +
 +    // code_UE_fille
 +    $cmp_code_UE_fille = strcmp($a->code_UE_fille, $b->code_UE_fille);
 +    $cmpAll += ($cmp_code_UE_fille == 0 ? 0 : ($cmp_code_UE_fille > 0 ? 1 : -1));
 +
 +    return $cmpAll;
 +});
 +</code>
 +
 +Variante PHP8
 +
 +<code php>
 +usort ($rows, function ($a, $b) {
 +    return (
 +        // code_etape . version
 +        ($a->code_etape . $a->version <=> $b->code_etape . $b->version) * 1000000 +
 +    
 +        // nature BLCC
 +        (($b->nature == 'BLCC' ? 1 : 0) - ($a->nature == 'BLCC' ? 1 : 0)) * 100000 +
 +
 +        // nature BLCA
 +        (($b->nature == 'BLCA' ? 1 : 0) - ($a->nature == 'BLCA' ? 1 : 0)) * 10000 +
 +
 +        // nature SEM
 +        (($b->nature == 'SEM' ? 1 : 0) - ($a->nature == 'SEM' ? 1 : 0)) * 1000 +
 +
 +        // nature
 +        ($a->nature <=> $b->nature) * 100 +
 +
 +        // code_UE_mere
 +        ($a->code_UE_mere <=> $b->code_UE_mere) * 10 +
 +
 +        // code_UE_fille
 +        ($a->code_UE_fille <=> $b->code_UE_fille)
 +    );
 +});
 +</code>
 +
 +Variante plus élégante
 +
 +<code php>
 +usort ($rows, function ($a, $b) {
 +    return (
 +        $p = 0;
 +        
 +        // code_UE_fille
 +        ($a->code_UE_fille <=> $b->code_UE_fille) * pow(10, $p++) +
 +        
 +        // code_UE_mere
 +        ($a->code_UE_mere <=> $b->code_UE_mere) * pow(10, $p++) +
 +        
 +        // nature
 +        ($a->nature <=> $b->nature) * pow(10, $p++) +
 +        
 +        // nature SEM
 +        (($b->nature == 'SEM' ? 1 : 0) - ($a->nature == 'SEM' ? 1 : 0)) * pow(10, $p++) +
 +        
 +        // nature BLCA
 +        (($b->nature == 'BLCA' ? 1 : 0) - ($a->nature == 'BLCA' ? 1 : 0)) * pow(10, $p++) +
 +        
 +        // nature BLCC
 +        (($b->nature == 'BLCC' ? 1 : 0) - ($a->nature == 'BLCC' ? 1 : 0)) * pow(10, $p++) +
 +        
 +        // code_etape . version
 +        ($a->code_etape . $a->version <=> $b->code_etape . $b->version) * pow(10, $p++) +
 +    );
 +});
 </code> </code>
  
Ligne 57: Ligne 172:
 ini_set("xdebug.var_display_max_depth",    '-1'); ini_set("xdebug.var_display_max_depth",    '-1');
 var_dump($var); var_dump($var);
 +</code>
 +
 +==== Divers ====
 +
 +=== Installer Composer rapidement ===
 +
 +<code bash>
 +curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
 </code> </code>