Jump to: navigation, search

Php array shift

From w3cyberlearnings

Contents

PHP function array_shift

This function returns the first array element, and it removes the first array element from the origional array.

Syntax array_shift

  • array: array input
array_shift(array);

Note

Use array_pop() to remove the last array element.

Example 1

<?php

$records = array('John', 'Zahra', 'Omari');
$first_element = array_shift($records);

echo $first_element; 
?>

Output

John

Example 2

<?php

$records = array('John', 'Zahra', 'Omari');
$first_element = array_shift($records);

print_r($records);
?>

Output

Array
(
    [0] => Zahra
    [1] => Omari
)

Example 3

<?php

$test = array('job' => 32, 'apple' => 'orange', 'pen' => 'paper');
$return =array_shift($test);
print_r($return);

?>

Output

32

Related Links


Navigation
Web
SQL
MISC
References