array_unshift() function in PHP


The array_unshift() function adds one or more elements to the beginning of an array.

Syntax

array_unshift(arr, val1, val2, val3)

Parameters

  • arr − The specified array. Required.

  • val1 − Value to be inserted. Required.

  • val2 − Value to be inserted. Optional.

  • val3 − Value to be inserted. Optional.

Return

The array_unshift() function returns the new elements in the array.

Example

The following is an example −

 Live Demo

<?php
$arr = array("p"=>"one","q"=>"two");
print_r(array_unshift($arr,"three"));
?>

Output

The following is the output −

3

Example

Let us see another example −

 Live Demo

<?php
$arr = array("p"=>"one","q"=>"two");
array_unshift($arr,"three");
print_r($arr);
?>

Output

The following is the output −

Array
(
[0] => three
[p] => one
[q] => two
)

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 23-Dec-2019

70 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements