Bifalgorithm post · Computer science one · Adding Elements

How can you iterate through an array using foreach?

How can you iterate through an array using foreach? (Computer science one · Adding Elements) — "For each key value pair:\n\nforeach($arr as $key => $val) {\n\n…

Ideas in this flowchart

  • PHP foreach itère sur les éléments d’un tableau.
  • L’itération clé-valeur donne accès à la clé et à la valeur de chaque élément.
  • foreach($arr as $key => $val) { print "$key maps to $val"; }
  • Le mot-clé as désigne les variables utilisées pour la clé et la valeur.
  • Les variables $key et $val peuvent être remplacées par tout nom légal.
  • Lorsque les clés ne sont pas nécessaires, une syntaxe abrégée permet d’itérer uniquement sur les valeurs.
  • foreach($arr as $val) { print "$val"; }
  • Chaque itération de la syntaxe abrégée affiche la valeur courante.
  • Chaque itération clé-valeur affiche la clé associée à sa valeur.
Feed/

How can you iterate through an array using foreach?

No cover

Channel

Computer science one

1 page

"For each key value pair:\n\nforeach($arr as $key => $val) {\n\nprint \"$key maps to $val\n\n\";\n\n}\n\nThis syntax gives you access to both the key and the value for each element in the array $arr. The keyword as is used to denote the variable names $key and $val that will be changed on each iteration of the loop. You need not use the identifiers $key and $val; you can use any legal variable names for the key/value variables. If you do not need the keys when iterating, you can use the following shorthand syntax.\n\nFor each value:\n\nforeach($arr as $val) {\n\nprint \"$val\n\n\";\n\n}"

Flowchart preview
👍
0
0 comments0 shares

Sign in to view and post comments.