PHP array_unshift() Function

In this chapter you will learn:

  1. Definition for PHP array_unshift() Function
  2. Syntax for PHP array_unshift() Function
  3. Parameter for PHP array_unshift() Function
  4. Note for PHP array_unshift() Function
  5. Note 2 for PHP array_unshift() Function
  6. Example - adds a value onto the start of the array
  7. Example - Adds two value onto the start of the array

Definition

The array_unshift() function adds a value onto the start of the array. This is the opposite of the array_shift() function.

Syntax

PHP array_unshift() Function has the following syntax.

int array_unshift ( array &arr, mixed var [, mixed ...] )

Parameter

ParameterIs RequiredDescription
arrayRequired.Array to be added
value1Required.Value to insert
value2Optional.Value to insert
value3Optional.Value to insert

Note

You can't add key/value pairs to associative arrays using array_unshift() or its counterpart, array_pop(). However, you can work around this by using array_merge().

Note 2

With array_unshift() and array_push(), if you include an array as one of the values to add, the array is added to the original array as an element, turning the original array into a multidimensional array:


<?PHP/*from j a va 2 s.c  o  m*/
$authors = array( "Java", "PHP", "CSS", "HTML" ); 
$newAuthors = array( "Javascript", "Python" ); 
echo array_push( $authors, $newAuthors ) . "\n";
print_r( $authors ); 
?>

If you instead want to add the elements of the array individually to the original array, use array_merge().

The code above generates the following result.

Example 1

Adds a value onto the start of the array


<?PHP/*  j a  v a 2 s .co  m*/
$firstname = "java2s.com";
$names = array("A", "B", "C", "D", "E");
array_unshift($names, $firstname);
print_r($names);
?>

The code above generates the following result.

Example 2

Adds two value onto the start of the array


<?PHP//j  a va 2 s.  co  m
$authors = array( "Java", "PHP", "CSS", "HTML" ); 
echo array_unshift( $authors, "Javascript", "Python" ) . "\n"; // Displays "6" 
print_r( $authors );       
?>

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. Definition for PHP array_values() Function
  2. Syntax for PHP array_values() Function
  3. Parameter for PHP array_values() Function
  4. Example - returns an array of all the values
Home » PHP Tutorial » PHP Array Functions
PHP array() function
PHP array_change_key_case() function
PHP array_chunk() function
PHP array_combine() function
PHP array_count_values() function
PHP array_diff() function
PHP array_diff_assoc() Function
PHP array_diff_key() function
PHP array_diff_uassoc() function
PHP array_diff_ukey() function
PHP array_fill() function
PHP array_fill_keys() function
PHP array_filter() function
PHP array_flip() function
PHP array_intersect() Function
PHP array_intersect_assoc() Function
PHP array_intersect_key() function
PHP array_intersect_uassoc() Function
PHP array_intersect_ukey() Function
PHP array_key_exists() Function
PHP array_keys() function
PHP array_map() Function
PHP array_merge() function
PHP array_merge_recursive() Function
PHP array_multisort() Function
PHP array_pad() Function
PHP array_pop() function
PHP array_product() Function
PHP array_push() function
PHP array_rand() function
PHP array_reduce() Function
PHP array_replace() Function
PHP array_replace_recursive() Function
PHP array_reverse() Function
PHP array_search() Function
PHP array_shift() Function
PHP array_slice() Function
PHP array_splice() Function
PHP array_sum() Function
PHP array_udiff() Function
PHP array_udiff_assoc() Function
PHP array_udiff_uassoc() Function
PHP array_uintersect() Function
PHP array_uintersect_assoc() Function
PHP array_uintersect_uassoc() Function
PHP array_unique() Function
PHP array_unshift() Function
PHP array_values() Function
PHP array_walk() Function
PHP array_walk_recursive() Function
PHP arsort() Function
PHP asort() Function
PHP compact() Function
PHP count() Function
PHP current() Function
PHP each() Function
PHP end() Function
PHP explode() Function
PHP extract() Function
PHP implode() Function
PHP in_array() Function
PHP key() Function
PHP krsort() Function
PHP ksort() Function
PHP list() Function
PHP natcasesort() Function
PHP natsort() Function
PHP next() Function
PHP pos() Function
PHP prev() Function
PHP range() Function
PHP reset() Function
PHP rsort() Function
PHP shuffle() Function
PHP sizeof() Function
PHP sort() Function
PHP uasort() Function
PHP uksort() Function
PHP usort() Function