«

»

Mar 21

Array Shift and Unshift

Array_Shift : Shift an element off the beginning of array.

Syntax :

array_shift ( $arrval )

array_shift() shifts the first value of the array off and returns it. Return the shift value or null if array is empty.

Example:

$arrval = array("test", "test1", "test2", "test3");
$shiftval = array_shift($arrval);
print_r($arrval);

Output:
array(
[0] => test1
[1] => test2
[2] => test3
)

print_r($shiftval);

Output:
array(
[0]=>test
)

Array_Unshift : Insert one or more elements to the beginning of an array. In Array_shift all inserted element stay in the same order and array key modified to start continue from zero.

Return the new number of element in the array.

$arrval = array("test", "test1");
array_unshift($arrval, "test2", "test3");
print_r($arrval);

 

Output:
Array(
[0] => test
[1] => test1
[2] => test2
[3] => test3

)

 

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>