Skip to content

PHP Array Functions: array_shift() and array_pop()

array_shift(): Delete the first element in the array. It also returns the value of the element that it deleted from the array contents.

array_pop(): Used to delete the last element in the array. It also returns the value of the element that it deleted from the array contents.


	$ARRAY_LIST = ["maria","alex","jason","bob"];

	echo "<pre>";
	print_r($ARRAY_LIST);
	echo "</pre>";

	array_shift($ARRAY_LIST);
	
	echo "<pre>";
	print_r($ARRAY_LIST);
	echo "</pre>";

output:

Array
(
    [0] => maria
    [1] => alex
    [2] => jason
    [3] => bob
)

 Array
(
    [0] => alex
    [1] => jason
    [2] => bob
) 

	$ARRAY_LIST = ["maria","alex","jason","bob"];

	echo "<pre>";
	print_r($ARRAY_LIST);
	echo "</pre>";

	array_pop($ARRAY_LIST);
	
	echo "<pre>";
	print_r($ARRAY_LIST);
	echo "</pre>";

output:

Array
(
    [0] => maria
    [1] => alex
    [2] => jason
    [3] => bob
)
 Array
(
    [0] => maria
    [1] => alex
    [2] => jason
) 

	$ARRAY_LIST = ["maria","alex",array("rick","allen"),"jason","bob"];

	echo "<pre>";
	print_r($ARRAY_LIST);
	echo "</pre>";

	array_pop($ARRAY_LIST[2]);
	
	echo "<pre>";
	print_r($ARRAY_LIST);
	echo "</pre>";

output:

Array
(
    [0] => maria
    [1] => alex
    [2] => Array
        (
            [0] => rick
            [1] => allen
        )

    [3] => jason
    [4] => bob
)
Array
(
    [0] => maria
    [1] => alex
    [2] => Array
        (
            [0] => rick
        )

    [3] => jason
    [4] => bob
)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: