Which PHP function inserts an element to the end of an array?

Use array_push() to add one or more elements to the end of an array. A shorter way for one element is $array[] = value.

<?php
$fruits = ["Apple", "Banana"];

array_push($fruits, "Mango", "Orange");
$fruits[] = "Grapes";

print_r($fruits);
?>

Output:

Tutorials Class - Output Window

Array
(
[0] => Apple
[1] => Banana
[2] => Mango
[3] => Orange
[4] => Grapes
)

To add an element at the start use array_unshift(). To remove the last or the first element use array_pop() and array_shift(). See PHP Array Functions.


Learn more about the similar topics:
Tutorials
No Content Found.
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.