What are some alternative methods to array_push() for adding elements to associative arrays in PHP?

When adding elements to associative arrays in PHP, the array_push() function is typically used for indexed arrays, but it cannot be directly used for associative arrays. To add elements to an associative array, you can simply assign a new key-value pair to the array using square brackets notation.

// Define an associative array
$assocArray = array(
    'key1' => 'value1',
    'key2' => 'value2'
);

// Add a new element to the associative array
$assocArray['key3'] = 'value3';

// Print the updated associative array
print_r($assocArray);