«

»

Mar 21

Array Push and Pop

Array_Push : Push one or more elements onto the end of array

Syntax:

array_push ( $array ,  $value1 [, $value2, $value3,... ] )

array_push() treats array as a stack, and pushes the passed variables onto the end of array.

Simple example:
$arr[]=$val;

In Above Example return the new number of element in array.

Example:

$arrval=array("test","test1");
array_push($arrval,"test3","test4");
print_r($arrval);

Output

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

Array_Pop :  Pop the element off the end of array.

Syntax:
array_pop ( $arrval )

array_pop() pops and returns the last value of the array, shortening the array by one element. if array is empty then return the null value.

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

output:

array(
[0] => test
[1] => test1
[2] => test2

)

print_r($testval)

output:

array(

[0] => 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>