array_popPOPS the element to the ending of an ARRAY.
STACK
It is a type of ORGANIZATIONAL ELEMENTS in which INSERT or REMOVAL operations always affect the LAST ELEMENT, known as the TOP of the STACK.
POP
It is the operation used to REMOVE the ELEMENT from the TOP of a STACK.
PUSH
It is the operation used to INSERT an ELEMENT into the TOP of a STACK.
This function pops and returns the value of the last element of $array treated as a STACK.
The length of $array is decreased by the one
This function will issue a Warning if the first argument is not an ARRAY.
<?php
mix array_pop ( arr &$array )
where,
$array = The given input ARRAY
?>
$array
The given input array.
Can be passed by reference.
EXERCISE
<?php
$var01 = array("K" => 2, "L" => 8,
"M" => 18, "N" => 32,
"O" => 32, "P" => 18, "Q" => 8);
echo 'The given ARRAY:<br>';
print_r($var01);
echo '<br>' . count($var01) . ' ELEMENTS';
if (array_pop($var01))
{
echo '<br><br><br>The resulting ARRAY:<br>';
print_r($var01);
echo '<br>' . count($var01) . ' ELEMENTS<br><br>';
}
else
{
echo '<br>Operation Impossible!';
}
?>
RESULT
The given ARRAY:
Array
(
[K] => 2
[L] => 8
[M] => 18
[N] => 32
[O] => 32
[P] => 18
[Q] => 8
)
7 ELEMENTS
The resulting ARRAY:
Array
(
[K] => 2
[L] => 8
[M] => 18
[N] => 32
[O] => 32
[P] => 18
)
6 ELEMENTS
EXERCISE
<?php
$var02 = array(1 => array(0,1), 2 => array(0,2));
echo 'The given ARRAY:<br>';
print_r($var02);
echo '<br>' . count($var02) . ' ELEMENTS';
if (array_pop($var02))
{
echo '<br><br><br>The resulting ARRAY:<br>';
print_r($var02);
$ct02 = count($var02);
if($ct02 == 1)
{
echo '<br>' . $ct02 . ' ELEMENT<br><br>';
}
else
{
echo '<br>' . $ct02 . ' ELEMENTS<br><br>';
}
}
else
{
echo '<br>Operation Impossible!';
}
?>
RESULT
The given ARRAY:
Array
(
[1] => Array
(
[0] => 0
[1] => 1
)
[2] => Array
(
[0] => 0
[1] => 2
)
)
2 ELEMENTS
The resulting ARRAY:
Array
(
[1] => Array
(
[0] => 0
[1] => 1
)
)
1 ELEMENT
EXERCISE
<?php
$var03 = array();
if(array_pop($var03))
{
print_r($var03);
}
else
{
echo 'Operation Impossible!';
}
?>
RESULT
Operation Impossible!
EXERCISE
<?php
array_pop($GLOBALS);
$empty_array = array();
$number = 5;
$str = "abc";
/* Various combinations of arrays
to be used for the test */
$mixed_array = array(
array(),
array( 1,2,3,4,5,6,7,8,9 ),
array( "One", "_Two", "Three", "Four", "Five" ),
array( 6, "six", 7, "seven", 8, "eight", 9, "nine" ),
array( "a" => "aaa", "A" => "AAA",
"c" => "ccc", "d" => "ddd", "e" => "eee" ),
array( "1" => "one", "2" => "two",
"3" => "three", "4" => "four", "5" => "five" ),
array( 1 => "one", 2 => "two", 3 => 7, 4 => "four", 5 => "five" ),
array( "f" => "fff", "1" => "one", 4 =>
6, "" => "blank", 2.4 => "float", "F" => "FFF",
"blank" => "", 3.7 => 3.7, 5.4 => 7, 6 => 8.6,
'5' => "Five", "4name" => "jonny",
"a" => NULL, NULL => 3 ),
array( 12, "name", 'age', '45' ),
array( array("oNe", "tWo", 4),
array(10, 20, 30, 40, 50), array() ),
array( "one" => 1, "one" => 2, "three" => 3,
3, 4, 3 => 33, 4 => 44, 5, 6,
5.4 => 54, 5.7 => 57,
"5.4" => 554, "5.7" => 557 )
);
/* Loop to test normal functionality
with different arrays inputs */
echo "Normal testing with various array inputs<br><br>";
$counter = 1;
foreach( $mixed_array as $sub_array )
{
echo "<br><br>Input Array for Iteration $counter is<br>";
print_r( $sub_array );
echo "<br><br>Output after Pop is:<br>";
var_dump( array_pop($sub_array) );
$counter++;
}
?>
EXERCISE
<?php
/* Various combinations of arrays
to be used for the test */
$mixed_array = [ [],
[ 1,2,3,4,5,6,7,8,9 ],
[ "One", "_Two", "Three", "Four", "Five" ],
[ 6, "six", 7, "seven", 8, "eight", 9, "nine" ],
[ "a" => "aaa", "A" => "AAA", "c" => "ccc",
"d" => "ddd", "e" => "eee" ],
[ "1" => "one", "2" => "two", "3" => "three",
"4" => "four", "5" => "five" ],
[ 1 => "one", 2 => "two", 3 => 7,
4 => "four", 5 => "five" ],
[ "f" => "fff", "1" => "one",
4 => 6, "" => "blank", 2.4 => "float", "F" => "FFF",
"blank" => "", 3.7 => 3.7,
5.4 => 7, 6 => 8.6, '5' => "Five",
"4name" => "jonny", "a" => NULL, NULL => 3 ],
[ 12, "name", 'age', '45' ],
[ ["oNe", "tWo", 4], [10, 20, 30, 40, 50], [] ],
[ "one" => 1, "one" => 2, "three" => 3, 3, 4,
3 => 33, 4 => 44, 5, 6,
5.4 => 54, 5.7 => 57,
"5.4" => 554, "5.7" => 557 ] ];
echo"Checking for internal array
pointer being reset when pop is called<br><br>";
echo "<br>Current Element is:<br>";
var_dump( current($mixed_array[1]) );
echo "<br><br>Next Element is:<br>";
var_dump( next($mixed_array[1]) );
echo "<br><br>Next Element is:<br>";
var_dump( next($mixed_array[1]) );
echo "<br><br>POPed Element is:<br>";
var_dump( array_pop($mixed_array[1]) );
echo "<br><br>Current Element after POP operation is: ";
var_dump( current($mixed_array[1]) );
?>