Concept
How can strings be used as indices in PHP arrays?
ComputerScienceOne / Non-Contiguous Indices
"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."
Related Ideas
- How can strings be used as indices in PHP arrays?ComputerScienceOne · Strings as Indices
- Can PHP array indices be non-contiguous?ComputerScienceOne · Strings as Indices
- How does key-value initialization define PHP array keys and values?ComputerScienceOne · Strings as Indices
- How do you create and index arrays in PHP?ComputerScienceOne · Creating Arrays
- How can you iterate over indexed and associative arrays in PHP?ComputerScienceOne · Iteration
- How can you extract array keys and values and compare arrays in PHP?ComputerScienceOne · Iteration
- Which PHP functions return an array’s keys and values?ComputerScienceOne · Useful Functions
- How can you print the contents of a PHP array in a readable format?ComputerScienceOne · Useful Functions