What is the method to add elements to an array in PHP?
To add elements to an array in PHP, you can use the following method:
- “Use the bracket syntax”
$fruits = array('apple', 'banana', 'orange');
$fruits[] = 'kiwi';
- Use the array_push() function:
$fruits = array('apple', 'banana', 'orange');
array_push($fruits, 'kiwi');
- Directly specify key-value pairs in an array:
$fruits = array('apple', 'banana', 'orange');
$fruits['key'] = 'kiwi';
These methods can all be used to add new elements to an array.