Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentes Révision précédente | |||
web:php:chap3 [2023/11/08 15:22] – supprimée - modification externe (Unknown date) 127.0.0.1 | web:php:chap3 [2023/11/08 15:22] (Version actuelle) – ↷ Page déplacée de php:chap3 à web:php:chap3 jcheron | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
+ | ====== Chapitre 3 : Les tableaux ====== | ||
+ | < | ||
+ | |||
+ | ===== - Création ===== | ||
+ | |||
+ | ==== - Tableau indexé et tableau associatif ==== | ||
+ | |||
+ | === Tableau indexé === | ||
+ | |||
+ | <sxh php> | ||
+ | <?php | ||
+ | $array = [5," | ||
+ | var_dump($array); | ||
+ | ?> | ||
+ | </ | ||
+ | |||
+ | **Produit le résultat :** | ||
+ | |||
+ | < | ||
+ | array (size=4) | ||
+ | 0 => int 5 | ||
+ | 1 => string ' | ||
+ | 2 => string ' | ||
+ | 3 => string ' | ||
+ | </ | ||
+ | === Tableau associatif === | ||
+ | <sxh php> | ||
+ | <?php | ||
+ | $array = [ | ||
+ | " | ||
+ | " | ||
+ | ]; | ||
+ | var_dump($array); | ||
+ | ?> | ||
+ | </ | ||
+ | |||
+ | **Produit le résultat :** | ||
+ | |||
+ | < | ||
+ | array (size=2) | ||
+ | ' | ||
+ | ' | ||
+ | </ | ||
+ | |||
+ | === Tout tableau est associatif === | ||
+ | |||
+ | <sxh php> | ||
+ | <?php | ||
+ | $array=[ | ||
+ | " | ||
+ | " | ||
+ | 1=>" | ||
+ | ]; | ||
+ | var_dump($array); | ||
+ | ?> | ||
+ | </ | ||
+ | |||
+ | **Produit le résultat :** | ||
+ | |||
+ | < | ||
+ | array (size=3) | ||
+ | ' | ||
+ | ' | ||
+ | 1 => string ' | ||
+ | </ | ||
+ | |||
+ | ===== - Manipulations ===== | ||
+ | |||
+ | ==== - Ajout d' | ||
+ | |||
+ | <sxh php> | ||
+ | <?php | ||
+ | $array=[]; | ||
+ | $array[]=" | ||
+ | $array[" | ||
+ | $array[" | ||
+ | var_dump($array); | ||
+ | ?> | ||
+ | </ | ||
+ | |||
+ | **Produit le résultat :** | ||
+ | |||
+ | < | ||
+ | array (size=3) | ||
+ | 0 => string ' | ||
+ | ' | ||
+ | ' | ||
+ | </ | ||
+ | |||
+ | ==== - Accès à un élément ==== | ||
+ | |||
+ | <sxh php> | ||
+ | <?php | ||
+ | echo $array[" | ||
+ | echo $array[0]; | ||
+ | ?> | ||
+ | </ | ||
+ | |||
+ | ==== - Suppression d' | ||
+ | |||
+ | Suppression d'un élément à partir de l' | ||
+ | <sxh php> | ||
+ | <?php | ||
+ | $array=[]; | ||
+ | $array[]=" | ||
+ | $array[" | ||
+ | $array[" | ||
+ | $array[]=" | ||
+ | $array[]=" | ||
+ | var_dump($array); | ||
+ | array_splice($array, | ||
+ | var_dump($array); | ||
+ | ?> | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | array (size=5) | ||
+ | 0 => string 'pos 1' (length=5) | ||
+ | ' | ||
+ | ' | ||
+ | 1 => string 'pos 2' (length=5) | ||
+ | 2 => string 'pos 3' (length=5) | ||
+ | |||
+ | array (size=4) | ||
+ | 0 => string 'pos 1' (length=5) | ||
+ | ' | ||
+ | 1 => string 'pos 2' (length=5) | ||
+ | 2 => string 'pos 3' (length=5) | ||
+ | </ | ||
+ | |||
+ | **Suppression dans un tableau indexé et réindexation :** | ||
+ | |||
+ | <sxh php> | ||
+ | <?php | ||
+ | $carre=[]; | ||
+ | for($i=0; | ||
+ | $carre[]=pow($i, | ||
+ | } | ||
+ | var_dump($carre); | ||
+ | ?> | ||
+ | </ | ||
+ | < | ||
+ | array (size=10) | ||
+ | 0 => int 0 | ||
+ | 1 => int 1 | ||
+ | 2 => int 4 | ||
+ | 3 => int 9 | ||
+ | 4 => int 16 | ||
+ | 5 => int 25 | ||
+ | 6 => int 36 | ||
+ | 7 => int 49 | ||
+ | 8 => int 64 | ||
+ | 9 => int 81 | ||
+ | </ | ||
+ | |||
+ | |||
+ | Suppression de l' | ||
+ | <sxh php> | ||
+ | <?php | ||
+ | unset($carre[5]); | ||
+ | var_dump($carre); | ||
+ | ?> | ||
+ | </ | ||
+ | il manque l' | ||
+ | < | ||
+ | array (size=9) | ||
+ | 0 => int 0 | ||
+ | 1 => int 1 | ||
+ | 2 => int 4 | ||
+ | 3 => int 9 | ||
+ | 4 => int 16 | ||
+ | | ||
+ | 6 => int 36 | ||
+ | 7 => int 49 | ||
+ | 8 => int 64 | ||
+ | 9 => int 81 | ||
+ | </ | ||
+ | |||
+ | Réindexation : | ||
+ | <sxh php> | ||
+ | <?php | ||
+ | $carre=array_values($carre); | ||
+ | var_dump($carre); | ||
+ | ?> | ||
+ | </ | ||
+ | < | ||
+ | array (size=9) | ||
+ | 0 => int 0 | ||
+ | 1 => int 1 | ||
+ | 2 => int 4 | ||
+ | 3 => int 9 | ||
+ | 4 => int 16 | ||
+ | 5 => int 36 | ||
+ | 6 => int 49 | ||
+ | 7 => int 64 | ||
+ | 8 => int 81 | ||
+ | </ | ||
+ | |||
+ | ===== - Parcours ===== | ||
+ | |||
+ | ==== Tableau indexé : for ==== | ||
+ | |||
+ | <sxh php> | ||
+ | $array=[" | ||
+ | for($i=0; | ||
+ | echo $array[$i]; | ||
+ | } | ||
+ | ?> | ||
+ | </ | ||
+ | |||
+ | |||
+ | ==== Tableau associatif : foreach ==== | ||
+ | |||
+ | <sxh php> | ||
+ | <?php | ||
+ | $array=[1=>" | ||
+ | foreach($array as $k=>$v){ | ||
+ | echo($k." | ||
+ | } | ||
+ | ?> | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | 1 : a | ||
+ | 2 : b | ||
+ | config : ok | ||
+ | </ | ||
+ | |||
+ | |||
+ | <sxh php> | ||
+ | <?php | ||
+ | $array=[1=>" | ||
+ | foreach($array as $v){ | ||
+ | echo($v."< | ||
+ | } | ||
+ | ?> | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | a | ||
+ | b | ||
+ | ok | ||
+ | </ | ||
+ | |||
+ | ===== - Fonctions ===== | ||
+ | |< 100% >| | ||
+ | ^Fonction ^Rôle ^ | ||
+ | |sizeof |Nombre d' | ||
+ | |array_keys |Retourne les clés | | ||
+ | |array_values |Retourne les valeurs | | ||
+ | |||
+ | [[http:// |