Stack PHP

How to use Stack  in PHP array ?

Stack implementation in PHP array,We know that stack is the method of adding on top and deletion also on the top ,that mean the element add first will be deleted in last,in php Stack implementation  can be done in two ways

To understand push and pop we take an array $month=array(\’jan\’,\’feb\’), initially added to element in it.($month is an array type variable.)

Push array : By this method we are adding element in array using array function array_push().now we have add a month \’mar\’ to do that we use  array_push() function in which we provide two parameter first is array name and second value .


array_push($month,\'mar\');
print_r($month);

print_r will return formatted output
This will add mar in $month array on 3rd index,as array_push() work on stack function therefore the element will add at the end of the array.

Pop array : By this method we are deleting element from array using array function array_pop().


array_pop($month);
print_r($month);

print_r will return formatted output

The array_pop() function deletes the last element of an array .as function work on stack function therefore the element will deleted at the end of the array. ie. the newest element will be deleted. In above example the mar will be deleted after using array_pop() from the array.

 

Scroll to Top