How can you add a new key to an array in PHP?
To add a new key to an array in PHP, you can simply assign a value to a new key in the array using the square bracket notation. This will automatically create the new key if it doesn't already exist.
// Create an array
$array = array("key1" => "value1", "key2" => "value2");
// Add a new key to the array
$array["key3"] = "value3";
// Print the updated array
print_r($array);
Related Questions
- In what scenarios would it be more beneficial to use sessions over cookies for user authentication in PHP?
- How can the short array syntax in JavaScript be utilized to improve code readability and efficiency, as mentioned in the thread?
- How can PHP functions like nl2br and str_replace be integrated into a text replacement script to automate the process effectively?