Concept
How can you add elements to an array?
ComputerScienceOne / Adding Elements
"You can easily add elements to an array by providing an index and using the assignment operator as in the previous examples. There are also several functions PHP defines that can insert elements to the beginning (array_unshift()), end (array_push()) or at an arbitrary position (array_splice()).\n\n$arr = array(10, 20, 30);\n\narray_unshift($arr, 15);\n\n//arr is now [15, 10, 20, 30]\n\narray_push($arr, 25);\n\n//arr is now [15, 10, 20, 30, 25]\n\n//insert 35 at index 3:\n\narray_splice($arr, 3, 0, 35);\n\n//arr is now [15, 10, 20, 35, 30, 25]\n\nAnother, simpler way of adding elements is to use the following syntax:"
Related Ideas
- How are elements added to the next available integer index in a PHP array?ComputerScienceOne · Removing Elements
- How do you create and index arrays in PHP?ComputerScienceOne · Creating Arrays
- Can PHP array indices be non-contiguous?ComputerScienceOne · Strings as Indices
- How can you count the elements in a PHP array?ComputerScienceOne · Useful Functions
- How can you print the contents of a PHP array in a readable format?ComputerScienceOne · Useful Functions
- How can key-value pairs be initialized in a PHP array?ComputerScienceOne · Non-Contiguous Indices
- How can strings be used as indices in PHP arrays?ComputerScienceOne · Strings as Indices
- How does unset() remove elements from a PHP array?ComputerScienceOne · Removing Elements