Bifalgorithm post · Computer science one · Non-Contiguous Indices

How can strings be used as indices in PHP arrays?

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

Ideas in this flowchart

  • PHP arrays support associative keys
  • PHP arrays are associative arrays
  • Integer values can be used as keys
  • Strings can be used as keys to index elements
  • The example creates and uses an array
  • $arr[0] = 5;
  • $arr["foo"] = 10; and $arr["hello"] = "world";
  • The value associated with "hello" is printed
  • Strings containing integer values are type-juggled into numeric keys
  • $arr["10"] = 3; is equivalent to $arr[10] = 3;
  • Strings containing floating-point values remain strings
  • $arr["3.15"] = 7; keeps "3.15" as a string key
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.