array_pop


php128 apg

POPS 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.



  1 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


  2 EXERCISE   

<?php

$var02 
= array(=> array(0,1), => 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


  3 EXERCISE   

<?php

$var03 
= array();

if(
array_pop($var03))
{
print_r($var03);
}
else
{
echo 
'Operation Impossible!';
}

?>

 RESULT   

Operation Impossible!

  4 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,),
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( 
=> "one"=> "two"=> 7=> "four"=> "five" ),
array( 
"f" => "fff""1" => "one"=> 
            
6"" => "blank"2.4 => "float""F" => "FFF",
            
"blank" => ""3.7 => 3.75.4 => 7=> 8.6
            
'5' => "Five""4name" => "jonny"
            
"a" => NULLNULL => ),
array( 
12"name"'age''45' ),
array( array(
"oNe""tWo"4), 
           array(
1020304050), array() ),
array( 
"one" => 1"one" => 2"three" => 3
            
34=> 33=> 4456,
            
5.4 => 545.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_dumparray_pop($sub_array) );
$counter++;
}

?>

  5 EXERCISE   

<?php

/* Various combinations of arrays 
             to be used for the test */
$mixed_array = [ [],
  [ 
1,2,3,4,5,6,7,8,],
  [ 
"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" ],
  [ 
=> "one"=> "two"=> 7
                             
=> "four"=> "five" ],
  [ 
"f" => "fff""1" => "one"
            
=> 6"" => "blank"2.4 => "float""F" => "FFF"
            
"blank" => ""3.7 => 3.7
            
5.4 => 7=> 8.6'5' => "Five"
            
"4name" => "jonny""a" => NULLNULL => ],
  [ 
12"name"'age''45' ],
  [ [
"oNe""tWo"4], [1020304050], [] ],
  [ 
"one" => 1"one" => 2"three" => 334
                      
=> 33=> 4456
                      
5.4 => 545.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_dumpcurrent($mixed_array[1]) );

echo 
"<br><br>Next Element is:<br>";
var_dumpnext($mixed_array[1]) );

echo 
"<br><br>Next Element is:<br>";
var_dumpnext($mixed_array[1]) );

echo 
"<br><br>POPed Element is:<br>";
var_dumparray_pop($mixed_array[1]) );

echo 
"<br><br>Current Element after POP operation is: ";
var_dumpcurrent($mixed_array[1]) );

?>