array_pop(): Taking out last element of the array

array_pop(): remove the last array value

array_pop() removes and returns the last value of an array. The input array is changed in place; an empty array returns null.

$items = ['red', 'green', 'blue'];
$removed = array_pop($items);
print_r($items);
echo $removed;

Use array_pop() when the collection behaves like a stack and you need the removed value.

$output=array_pop($input);
array_pop() to remove last element of the array
Take out the last element of the array and returns the same. After applying array_pop() total elements of the array reduces by one. All numerical array keys remain same as last element is removed. Literal keys remain same.

Returns the shifted element ( last element ) of the array.

Here $input is an array, $output get the value of last element of the array.
$input=array('One','Two','Three','Four');
$output=array_pop($input);
echo $output;// Output : Four
echo "<br><br>";
foreach ($input as $key => $val) {
echo "$key -> $val <br>";
}
Output of above code is here( $output gets the last element Four )
Four

0 -> One 
1 -> Two 
2 -> Three  

With Literal Keys

$input=array('Fruits1' =>'Banana','Fruits2'=>'Mango','Fruits3'=>'Apple','Fruits4'=>'Grapes');
$output=array_pop($input);
echo $output;// Output : Grapes
echo "<br><br>";
foreach ($input as $key => $val) {
echo "$key -> $val <br>";
}
Output is here, ( $output gets the first element Grapes)
Grapes

Fruits1 -> Banana 
Fruits2 -> Mango 
Fruits3 -> Apple 

Using end & current

By using current() we can get the element to which the present internal pointer is pointing. We can move the pointer to last element by using end.
$input=array('Fruits1' =>'Banana','Fruits2'=>'Mango','Fruits3'=>'Apple','Fruits4'=>'Grapes');
end($input);
echo current($input); 
echo "<br>";
$output=array_pop($input);
end($input);
echo current($input); 
Output is here.( The last element is changed from Grapes to Apple)
Grapes
Apple
Joining Two Arrays by array_merge
Array REFERENCE


Subscribe to our YouTube Channel here



plus2net.com











PHP video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer