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 do you create and index arrays in PHP?ComputerScienceOne · Creating Arrays
- How do you create and populate a two-dimensional integer array?ComputerScienceOne · Dynamic Data Structures
- How are elements added to the next available integer index in a PHP array?ComputerScienceOne · Removing Elements
- How can you iterate over Java arrays?ComputerScienceOne · Basic Usage
- How do you access array elements safely in C?ComputerScienceOne · Basic Usage