How to Remove Last Element from Array in PHP?

By Hardik Savani May 14, 2024 Category : PHP

This example is focused on how to remove last element from array in php. We will look at example of remove last item from array in php. if you want to see example of php remove last element from array then you are a right place. i would like to show you php remove last item from array. Let's see bellow example php remove last n element from array.

Let's see simple two example single dimensional array and multi dimensional array how to remove last item from array using php array function call array_pop(). array_pop() will helps to remove last key from php array.

Example 1:

index.php

<?php

$myArray = ["Apple", "Banana", "Mango"];

array_pop($myArray);

print_r($myArray);

?>

Output:

Array

(

[0] => Apple

[1] => Banana

)

Example 1:

index.php

<?php

$students = [

[ "id" => 1, "name" => "Hardik"],

[ "id" => 2, "name" => "Haresh"],

[ "id" => 3, "name" => "Naresh"],

];

array_pop($students);

print_r($students);

?>

Output:

Array

(

[0] => Array

(

[id] => 1

[name] => Hardik

)

[1] => Array

(

[id] => 2

[name] => Haresh

)

)

i hope it can help you...

Tags :
Shares