Bifalgorithm post · Computer science one · Strings as Indices

How can strings be used as indices in PHP arrays?

How can strings be used as indices in PHP arrays? (Computer science one · Strings as Indices) — "Since arrays in PHP are associative arrays, keys are not limit…

Ideas in this flowchart

  • Les tableaux PHP sont des tableaux associatifs
  • Les clés ne sont pas limitées aux entiers
  • Les chaînes peuvent servir de clés pour indexer les éléments
  • Exemples d'utilisation du tableau
  • $arr = array(); initialise le tableau
  • $arr[0] = 5$, $arr["foo"] = 10$ et $arr["hello"] = "world"$ ajoutent des éléments
  • print "value = " . $arr["hello"] affiche la valeur associée à la clé « hello »
  • Les chaînes contenant des valeurs entières sont converties en valeurs numériques
  • $arr["10"] = 3$ équivaut à $arr[10] = 3$
  • Les chaînes contenant des valeurs à virgule flottante restent des chaînes
  • $arr["3.15"] = 7$ conserve « 3.15 » comme clé chaîne
Feed/

How can strings be used as indices in PHP arrays?

No cover

Channel

Computer science one

1 page

"Since arrays in PHP are associative arrays, keys are not limited to integers. You can also use strings as keys to index elements.\n\n1 $arr = array();\n\n2 $arr[0] = 5;\n\n3 $arr[\"foo\"] = 10;\n\n4 $arr[\"hello\"] = \"world\";\n\n5\n\n6 print \"value = \" . $arr[\"hello\"];\n\nNote that strings that contain integer values will be type-juggled into their numeric values. For example, $arr[\"10\"] = 3; will be equivalent to $arr[10] = 3;. However, strings containing floating point values will not be coerced but will remain as strings, $arr[\"3.15\"] = 7; for example."

Flowchart preview
👍
0
0 comments0 shares

Sign in to view and post comments.