Array push creates new index php
In PHP, when you use the array_push()
function to add one or more elements to the end of an array, it does not create a new index. Instead, it appends the new elements to the existing array, and the new elements are assigned the next available index.
Here's an example:
$array = array('a', 'b', 'c');
array_push($array, 'd', 'e', 'f');
print_r($array);
Output:
Array
(
[0] => a
[1] => b
[2] => c
[3] => d
[4] => e
[5] => f
)
As you can see, the new elements d
, e
, and f
are added to the end of the array, and they are assigned indices 3, 4, and 5, respectively.
If you want to create a new array with a new index, you can use the array_merge()
function instead:
$array = array('a', 'b', 'c');
$new_array = array_merge($array, array('d', 'e', 'f'));
print_r($new_array);
Output:
Array
(
[0] => a
[1] => b
[2] => c
[0] => d
[1] => e
[2] => f
)
In this case, the new array new_array
is created with a new index, and the elements from the original array are copied to the new array.