Différences
Ci-dessous, les différences entre deux révisions de la page.
| Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
| kb_it:php [2022/07/06 11:36] – befe | kb_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); | ||
| } | } | ||
| + | </ | ||
| + | |||
| + | === Rechercher dans un tableau associatif === | ||
| + | |||
| + | <code php> | ||
| + | $people = array( | ||
| + | 2 => array( | ||
| + | ' | ||
| + | ' | ||
| + | ), | ||
| + | 5=> array( | ||
| + | ' | ||
| + | ' | ||
| + | ) | ||
| + | ); | ||
| + | $found_key = array_search(' | ||
| + | </ | ||
| + | |||
| + | 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, | ||
| + | $found_key = array_search(' | ||
| + | </ | ||
| + | |||
| + | === Remplacer les clés d'un tableau === | ||
| + | |||
| + | <code php> | ||
| + | $array = [' | ||
| + | $keys = [' | ||
| + | |||
| + | $newArray = array_combine(array_map(function($el) use ($keys) { | ||
| + | return $keys[$el]; | ||
| + | }, array_keys($array)), | ||
| + | </ | ||
| + | |||
| + | === Tri multi-critères === | ||
| + | |||
| + | <code php> | ||
| + | usort ($rows, function ($a, $b) { | ||
| + | $cmpAll = 0; | ||
| + | |||
| + | // code_etape . version | ||
| + | $cmp_code_etape_version = strcmp($a-> | ||
| + | $cmpAll += $cmp_code_etape_version == 0 ? 0 : ($cmp_code_etape_version > 1 ? 1 : -1) * 1000000; | ||
| + | |||
| + | // nature BLCC | ||
| + | $cmpAll += (($b-> | ||
| + | |||
| + | // nature BLCA | ||
| + | $cmpAll += (($b-> | ||
| + | |||
| + | // nature SEM | ||
| + | $cmpAll += (($b-> | ||
| + | |||
| + | // code_UE_mere | ||
| + | $cmp_code_UE_mere = strcmp($a-> | ||
| + | $cmpAll += ($cmp_code_UE_mere == 0 ? 0 : ($cmp_code_UE_mere > 0 ? 1 : -1)) * 10; | ||
| + | |||
| + | // code_UE_fille | ||
| + | $cmp_code_UE_fille = strcmp($a-> | ||
| + | $cmpAll += ($cmp_code_UE_fille == 0 ? 0 : ($cmp_code_UE_fille > 0 ? 1 : -1)); | ||
| + | |||
| + | return $cmpAll; | ||
| + | }); | ||
| + | </ | ||
| + | |||
| + | Variante PHP8 | ||
| + | |||
| + | <code php> | ||
| + | usort ($rows, function ($a, $b) { | ||
| + | return ( | ||
| + | // code_etape . version | ||
| + | ($a-> | ||
| + | | ||
| + | // nature BLCC | ||
| + | (($b-> | ||
| + | |||
| + | // nature BLCA | ||
| + | (($b-> | ||
| + | |||
| + | // nature SEM | ||
| + | (($b-> | ||
| + | |||
| + | // nature | ||
| + | ($a-> | ||
| + | |||
| + | // code_UE_mere | ||
| + | ($a-> | ||
| + | |||
| + | // code_UE_fille | ||
| + | ($a-> | ||
| + | ); | ||
| + | }); | ||
| + | </ | ||
| + | |||
| + | Variante plus élégante | ||
| + | |||
| + | <code php> | ||
| + | usort ($rows, function ($a, $b) { | ||
| + | return ( | ||
| + | $p = 0; | ||
| + | | ||
| + | // code_UE_fille | ||
| + | ($a-> | ||
| + | | ||
| + | // code_UE_mere | ||
| + | ($a-> | ||
| + | | ||
| + | // nature | ||
| + | ($a-> | ||
| + | | ||
| + | // nature SEM | ||
| + | (($b-> | ||
| + | | ||
| + | // nature BLCA | ||
| + | (($b-> | ||
| + | | ||
| + | // nature BLCC | ||
| + | (($b-> | ||
| + | | ||
| + | // code_etape . version | ||
| + | ($a-> | ||
| + | ); | ||
| + | }); | ||
| </ | </ | ||
| Ligne 39: | Ligne 165: | ||
| ==== Débugging ==== | ==== Débugging ==== | ||
| - | === var_dump() sur de longues | + | === var_dump() sur de longues |
| <code php> | <code php> | ||
| Ligne 46: | Ligne 172: | ||
| ini_set(" | ini_set(" | ||
| var_dump($var); | var_dump($var); | ||
| + | </ | ||
| + | |||
| + | ==== Divers ==== | ||
| + | |||
| + | === Installer Composer rapidement === | ||
| + | |||
| + | <code bash> | ||
| + | curl -sS https:// | ||
| </ | </ | ||