PHP/Data Structure/array pop
array_pop( ) returns the value from the end of the array while also removing it from the array.
//mixed array_pop ( array &arr )
<?
$names = array("Timmy", "Bobby", "Sam", "Tammy", "Joe");
$firstname = array_pop($names);
?>
array to comma string
<?
function array_to_comma_string($array) {
switch (count($array)) {
case 0:
return "";
case 1:
return reset($array);
case 2:
return join(" and ", $array);
default:
$last = array_pop($array);
return join(", ", $array) . ", and $last";
}
}
?>
Remove array elements
<html>
<head>
<title>Remove array elements</title>
</head>
<body>
<ol>
<?php
$arr = array( "Orange", "Cherry", "Apple", "Banana", "Lemon" );
$first = array_shift( $arr );
$last = array_pop( $arr );
sort( $arr );
foreach( $arr as $value){ echo("$value, ");}
echo("<br>Removed first element: $first");
echo("<br>Removed last element: $last");
?>
</ol>
</body>
</html>