Skip to content

Instantly share code, notes, and snippets.

@tomsihap
Last active August 29, 2023 20:50
Show Gist options
  • Select an option

  • Save tomsihap/0ce95ee46a6b57d55144a67d68baed35 to your computer and use it in GitHub Desktop.

Select an option

Save tomsihap/0ce95ee46a6b57d55144a67d68baed35 to your computer and use it in GitHub Desktop.
PHP Exercises: arrays

Array Exercises (PHP)

Questions

  1. Soit un tableau $a = array( 0, 1, 2, 3, 4 );, comment afficher la valeur 3 du tableau ?

  2. Afficher la valeur 3 du tableau suivant :

$a = [
  "zero"  => 0,
  "un"    => 1,
  "deux"  => 2,
  "trois" => 3,
  "quatre"=> 4,
];
  1. Afficher la valeur 3 du tableau suivant :
$a = [
  [0, 1],
  [2,[3]],
  "zero"  => 0,
  "un"    => 1,
  "deux"  => 2,
  "quatre"=> 4,
];
  1. Afficher la valeur 3 du tableau suivant :
$a = [
    "a" => [
        "b" => 0,
        "c" => 1,
    ],
    "d" => [
        "e" =>  2,
        "f" => [
            "g" => 3
        ]
    ]
];
  1. Terreur ! Un élève a oublié d'indenter son code. Indentez cet array correctement et corrigez l'erreur.
$a = [ "a" => [
   "b" => 0,  "c" => 1,],"d" => [
 2, "f" =>    [
   "g" => 3
 ]];
  1. Trouver la somme de cet tableau de nombres : $a = [ 0, 1, 2, 3, 4, 5, 6 ];.

  2. Créez un array $films contenant les données suivantes :

- The Shawshank Redemption
    Année : 1994
    Note : 9.2		
- The Godfather
    Année : 1972
    Note : 9.1		
- The Dark Knight
    Année : 2008
    Note : 9.0		
- The Lord of the Rings: The Return of the King
    Année : 2003
    Note : 8.9
- Pulp Fiction
    Année : 1994
    Note : 8.9

8a. Créez le tableau suivant :

- The Shawshank Redemption	
- The Godfather
- The Dark Knight
- The Lord of the Rings: The Return of the King
- Pulp Fiction

8b. Affichez tous ses éléments grâce à une boucle foreach($films as $film) { /*...*/ } :

Exemple :

foreach($saisons as $saison) {
  var_dump($saison);
}

8c. Affichez, toujours grâce à une boucle, plutôt avec à un echo. N'oubliez pas de sauter une ligne entre chaque film !

Exemple :

// Afficher du PHP et du HTML en même temps :

// Solution 1 : concaténation
<body>
  <?php
  echo "<h1>" . $prenom . "</h1>";
  ?>
</body>

// Solution 2 : PHP dans HTML
<body>
  <h1><?php echo $prenom; ?></h1>
</body>

8d. Affichez ces éléments, toujours grâce à une boucle, plutôt dans une liste <ul> <li>.

9a. Soit le tableau suivant :

- The Shawshank Redemption : Frank Darabont
- The Godfather : Francis Ford Coppola
- The Dark Knight : Christopher Nolan
- The Lord of the Rings: The Return of the King : Peter Jackson
- Pulp Fiction : Quentin Tarantino

Créez un tableau de clés => valeurs avec ce tableau, avec le film en clé et le réalisateur en valeur rappel :

$array = [
    "cle" => "valeur",
    "cle2" => "valeur2",
]

9b. Reprennez le tableau de l'exercice 9a. Grâce à une boucle dans une boucle, affichez le tableau dans une liste <ul> <li> toutes les informations affichées de la façon suivante :

  • The Shawshank Redemption : Frank Darabont
  • The Godfather : Francis Ford Coppola
  • The Dark Knight : Christopher Nolan
  • The Lord of the Rings: The Return of the King : Peter Jackson
  • Pulp Fiction : Quentin Tarantino

Exemple :

// Soit un tableau :

$saisons = [
  "ete" => "juin",
  "automne" => "septembre",
  "hiver" => "decembre",
  "printemps" => "mars"
];

// Il s'agit d'un tableau de clés/valeurs.
// La clé : le nom de la saison
// La valeur : le tableau contenant les détails de la saison

// Dans ce cas, on précise dans le foreach qu'il s'agit non plus d'une simple valeur (comme l'exemple ci-dessus, $saisons)
// mais d'un ensemble de clés => valeurs : $saison => $detail

// Ainsi, on a accès à la clé avec $saison, et au détail avec $detail

foreach($saisons as $saison => $detail) {

  // Afficher la saison :
  echo $saison;
  
  // Afficher le détail :
  echo $detail;

  // Afficher les deux en concaténation sous la forme "la saison $saison débute en $detail"
  echo "La saison " $saison . " débute en ". $detail;
}

9c. Reprennez le tableau de l'exercice 6. Grâce à une boucle dans une boucle, affichez le tableau dans une liste <ul> <li> toutes les informations affichées de la façon suivante :

- The Shawshank Redemption
    Année : 1994
    Note : 9.2		
- The Godfather
    Année : 1972
    Note : 9.1		
- The Dark Knight
    Année : 2008
    Note : 9.0		
- The Lord of the Rings: The Return of the King
    Année : 2003
    Note : 8.9
- Pulp Fiction
    Année : 1994
    Note : 8.9

Exemple

$saisons = [
    "ete" => [
        "debut" => "juin",
        "fin"   => "septembre",
    ],
    "hiver" => [
        "debut" => "decembre",
        "fin"   => "mars",
    ]
];


foreach ($saisons as $saison => $details) {

    // Afficher la saison
    echo $saison;

    // Afficher le détail de chaque saison, on boucle le sous-tableau $details :

    foreach ($details as $detail) {

        // Afficher le début de la saison
        echo $detail['debut'];

        // Afficher la fin de la saison
        echo $detail['fin'];
    }

}
@AlexBuathier
Copy link

Nice !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment