Adding multiple variables to beginning of an array using array_unshift()

array_unshift() prepends passed elements to the front of the array. The array_unshift() function, accepts an array and any number of additional values. It will add the value arguments to the start of the given array.

<?php
 
$alpha = array("a", "b", "c");
$final = array_unshift($alpha, "d","e");
 
 
print "There are $total elements in \$alpha<p>";
foreach ( $alpha as $val ) {
  print "$val<br/>";
}
print "</p>";
?>

The output is an array containing d,e,a,b,c respectively positioned.

array_unshift() returns the new size of the array it transforms.

Share Article/Example:
  • DotNetKicks
  • DZone
  • StumbleUpon
  • Print
  • Add to favorites
  • Digg
  • del.icio.us
  • Twitter
  • Facebook
  • LinkedIn
  • Posterous
  • Slashdot

 

 
eXTReMe Tracker