web:php:chap3

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édentes Révision précédente
web:php:chap3 [2023/11/08 15:22] – supprimée - modification externe (Unknown date) 127.0.0.1web: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 ======
  
 +<html><div class="towrite">Les tableaux : <ul><li>leur rôle</li><li>leur utilisation</li></ul></div></html>
 +
 +===== - Création =====
 +
 +==== - Tableau indexé et tableau associatif ====
 +
 +=== Tableau indexé ===
 +
 +<sxh php>
 +<?php
 +$array = [5,"est","un","entier"];
 +var_dump($array);
 +?>
 +</sxh>
 +
 +**Produit le résultat :**
 +
 +<code>
 +array (size=4)
 +  0 => int 5
 +  1 => string 'est' (length=3)
 +  2 => string 'un' (length=2)
 +  3 => string 'entier' (length=6)
 +</code>
 +=== Tableau associatif ===
 +<sxh php>
 +<?php
 +$array = [
 +    "foo" => "bar",
 +    "bar" => "foo"
 +];
 +var_dump($array);
 +?>
 +</sxh>
 +
 +**Produit le résultat :**
 +
 +<code>
 +array (size=2)
 +  'foo' => string 'bar' (length=3)
 +  'bar' => string 'foo' (length=3)
 +</code>
 +
 +=== Tout tableau est associatif ===
 +
 +<sxh php>
 +<?php
 +$array=[
 +    "foo" => "bar",
 +    "bar" => "foo",
 +    1=>"indexé"
 +];
 +var_dump($array);
 +?>
 +</sxh>
 +
 +**Produit le résultat :**
 +
 +<code>
 +array (size=3)
 +  'foo' => string 'bar' (length=3)
 +  'bar' => string 'foo' (length=3)
 +  1 => string 'indexé' (length=7)
 +</code>
 +
 +===== - Manipulations =====
 +
 +==== - Ajout d'éléments ====
 +
 +<sxh php>
 +<?php
 +$array=[];
 +$array[]="indexé";
 +$array["foo"]="bar";
 +$array["bar"]="foo";
 +var_dump($array);
 +?>
 +</sxh>
 +
 +**Produit le résultat :**
 +
 +<code>
 +array (size=3)
 +  0 => string 'indexé' (length=7)
 +  'foo' => string 'bar' (length=3)
 +  'bar' => string 'foo' (length=3)
 +</code>
 +
 +==== - Accès à un élément ====
 +
 +<sxh php>
 +<?php
 +echo $array["foo"];
 +echo $array[0];
 +?>
 +</sxh>
 +
 +==== - Suppression d'éléments ====
 +
 +Suppression d'un élément à partir de l'index 2 :
 +<sxh php>
 +<?php
 +$array=[];
 +$array[]="pos 1";
 +$array["foo"]="bar";
 +$array["bar"]="foo";
 +$array[]="pos 2";
 +$array[]="pos 3";
 +var_dump($array);
 +array_splice($array, 2, 1);
 +var_dump($array);
 +?>
 +</sxh>
 +
 +<code>
 +array (size=5)
 +  0 => string 'pos 1' (length=5)
 +  'foo' => string 'bar' (length=3)
 +  'bar' => string 'foo' (length=3)
 +  1 => string 'pos 2' (length=5)
 +  2 => string 'pos 3' (length=5)
 +
 +array (size=4)
 +  0 => string 'pos 1' (length=5)
 +  'foo' => string 'bar' (length=3)
 +  1 => string 'pos 2' (length=5)
 +  2 => string 'pos 3' (length=5)
 +</code>
 +
 +**Suppression dans un tableau indexé et réindexation :**
 +
 +<sxh php>
 +<?php
 +$carre=[];
 +for($i=0;$i<10;$i++){
 + $carre[]=pow($i,2);
 +}
 +var_dump($carre);
 +?>
 +</sxh>
 +<code>
 +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
 +</code>
 +
 +
 +Suppression de l'élément d'indice 5 (le 6ème élément) :
 +<sxh php>
 +<?php
 +unset($carre[5]);
 +var_dump($carre);
 +?>
 +</sxh>
 + il manque l'indice 5 dans le tableau
 +<code>
 +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
 +</code>
 +
 +Réindexation :
 +<sxh php>
 +<?php
 +$carre=array_values($carre);
 +var_dump($carre);
 +?>
 +</sxh>
 +<code>
 +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
 +</code>
 +
 +===== - Parcours =====
 +
 +==== Tableau indexé : for ====
 +
 +<sxh php>
 +$array=["a","b","c"];
 +for($i=0;$i<count($array);$i++){
 +    echo $array[$i];
 +}
 +?>
 +</sxh>
 +
 +
 +==== Tableau associatif : foreach ====
 +
 +<sxh php>
 +<?php
 +$array=[1=>"a",2=>"b","config"=>"ok"];
 +foreach($array as $k=>$v){
 +    echo($k." : ".$v."<br>");
 +}
 +?>
 +</sxh>
 +
 +<code>
 +1 : a
 +2 : b
 +config : ok
 +</code>
 +
 +
 +<sxh php>
 +<?php
 +$array=[1=>"a",2=>"b","config"=>"ok"];
 +foreach($array as $v){
 +    echo($v."<br>");
 +}
 +?>
 +</sxh>
 +
 +<code>
 +a
 +b
 +ok
 +</code>
 +
 +===== - Fonctions =====
 +|< 100% >|
 +^Fonction ^Rôle ^
 +|sizeof |Nombre d'éléments |
 +|array_keys |Retourne les clés |
 +|array_values |Retourne les valeurs |
 +
 +[[http://ir1.php.net/manual/fr/book.array.php|Fonctions relatives aux tableaux]]