Removing the First or Last Element from an Array : array_shift « Data Structure « PHP






Removing the First or Last Element from an Array

 
<?php
 
  $languages = array('French','German','Russian','Chinese','Hindi', 'Quechua');

  printf("<p>Original array:</p><pre>%s</pre>\n", var_export($languages, TRUE));

  $removed = array_shift($languages);
  printf("<p>Using array_shift():<br />Removed element: %s</p><pre>%s</pre>\n",
          $removed, var_export($languages, TRUE));
          
  $removed = array_pop($languages);
  printf("<p>Using array_pop():<br />Removed element: %s</p><pre>%s</pre>\n",
          $removed, var_export($languages, TRUE));
          
  unset( $languages[count($languages) - 1] );
  printf("<p>Using unset() and count():</p><pre>%s</pre>\n",
          var_export($languages, TRUE));
?>
  
  








Related examples in the same category

1.Removing the First Element of an Array with array_shift()
2.array_shift( ) function returns the value from the front of the array while also removing it from the array.
3.array_shift() function operates much like array_pop()