To add a new key-value pair to a map (or associative array) in PHP, you can use the following syntax:
main.php116 chars8 lines
In this example, we have a map $myMap
with three key-value pairs. To add a new key-value pair, we simply use the square bracket notation and assign a value to a new key, in this case "newKey" with the value "newValue".
We can then access the new key-value pair like any other in the map:
main.php45 chars2 lines
Alternatively, we can also use the array_push()
function to add a new key-value pair to the end of the map:
main.php51 chars2 lines
However, this is generally less common and not as efficient as assigning a new key-value pair directly with the square bracket notation.
gistlibby LogSnag