PHP/Data Structure/array pop

Материал из Web эксперт
Перейти к: навигация, поиск

array_pop( ) returns the value from the end of the array while also removing it from the array.

   <source lang="html4strict">

//mixed array_pop ( array &arr ) <?

   $names = array("Timmy", "Bobby", "Sam", "Tammy", "Joe");
   $firstname = array_pop($names);

?>

 </source>
   
  


array to comma string

   <source lang="html4strict">

<? 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";
   }

} ?>

 </source>
   
  


Remove array elements

   <source lang="html4strict">

<html>

<head>
 <title>Remove array elements</title>
</head>
<body>
    <?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("
    Removed first element: $first"); echo("
    Removed last element: $last");  ?>
</body>

</html>

 </source>