In PHP, the array methods like array_push and array_pop are used to add or remove elements or items from the array. The array_push method can use to add or insert one or more items or elements from the end of an array. and The array_pop() method can use to extract, delete and remove the elements/items from the end of the array.
array_push() function
PHP array_push() is a built-in function used to insert new elements at the end of an array and get the updated array elements. The array_push() method takes a single element or an array of elements and appends it to the array.
Syntax:-
array_push(array,value1,value2...)
Parameters
An array parameter is required to which we will add the value.
The value1 parameter is also required, which is the value we will insert into the specified array.
The value2, value3, and so on are optional parameters. However, we need to pass those parameters if we want to add multiple values.
Example:-
<?php
$nicname = ["Vijay", "Amit", "Dharmu"];
array_push($nicname, "Ravi", "Rahul");
echo "<pre>";
print_r($nicname);
echo "</pre>";
?>
Output:-
array_pop() function
PHP array_pop()
function to remove an element or value from the end of an array. The array_pop()
function also returns the last value of an array. However, if the array is empty (or the variable is not an array), the returned value will be NULL
.
Syntax:-
array_pop(array);
Example:-
<?php
$nicname = ["Vijay", "Amit", "Dharmu", "rahul"];
array_pop($nicname);
echo "<pre>";
print_r($nicname);
echo "</pre>";
?>
Output:-