array_pushPUSHES one or more elements 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.
PUSH
It is the operation used to INSERT an ELEMENT into the TOP of a STACK.
POP
It is the operation used to REMOVE the ELEMENT from the TOP of a STACK.
This function treats the given $array as a STACK, and pushes the passed variables onto the end of $array.
The length of $array is increased by the number of variables to be pushed.
This function will issue a Warning if the first argument is not an ARRAY.
<?php
int array_push ( arr &$array [, mix $val1, ..., $valN ] )
where,
$array = The given input ARRAY
$val1 = First value to push
...
$valN = Nth value to push
?>
$array
The given input array.
Can be passed by reference.
$val1
The first value to be pushed.
$valN
The nth value to be pushed.
EXERCISE
<?php
echo 'The given ARRAY:<br>';
$arr01 = array(2, 4, 6, 8, 10);
print_r($arr01);
echo '<br>' . count($arr01) . ' ELEMENTS<br><br><br>';
// ELEMENTS TO INSERT
$val01a = 12;
$val01b = 14;
$val01c = 16;
$new01 = array_push($arr01, $val01a, $val01b, $val01c);
echo 'Resulting ARRAY:<br>';
print_r($arr01);
echo '<br>' . $new01 . ' ELEMENTS';
?>
RESULT
The given ARRAY:
Array
(
[0] => 2
[1] => 4
[2] => 6
[3] => 8
[4] => 10
)
5 ELEMENTS
Resulting ARRAY:
Array
(
[0] => 2
[1] => 4
[2] => 6
[3] => 8
[4] => 10
[5] => 12
[6] => 14
[7] => 16
)
8 ELEMENTS
EXERCISE
<?php
$arr02 = ['two' => 2, 'four' => 4,
'six' => 6, 'eight' => 8, 'ten' => 10];
echo 'The given ARRAY:<br>';
print_r($arr02);
echo '<br><br>' . count($arr02) . ' ELEMENTS<br><br><br>';
$new02 = array_push($arr02, 12, 14, 16);
echo 'Resulting ARRAY:<br>';
print_r($arr02);
echo '<br>' . $new02 . ' ELEMENTS';
?>
RESULT
The given ARRAY:
Array
(
[two] => 2
[four] => 4
[six] => 6
[eight] => 8
[ten] => 10
)
5 ELEMENTS
Resulting ARRAY:
Array
(
[two] => 2
[four] => 4
[six] => 6
[eight] => 8
[ten] => 10
[0] => 12
[1] => 14
[2] => 16
)
8 ELEMENTS
EXERCISE
<?php
$arr03 = ['two' => 2, 'four' => 4,
'six' => 6, 'eight' => 8, 'ten' => 10];
echo 'The given ARRAY:<br>';
print_r($arr03);
echo '<br>' . count($arr03) . ' ELEMENTS<br><br><br>';
// TO INSERT
$arr03a = ['twelve' => 12, 'fourteen' => 14, 'sixteen' => 16];
echo 'The ARRAY to insert:<br>';
print_r($arr03a);
echo '<br>' . count($arr03a) . ' ELEMENTS<br><br><br>';
$new03 = array_push($arr03, $arr03a);
echo 'The result ARRAY:<br>';
print_r($arr03);
echo '<br>' . $new03 . ' ELEMENTS';
?>
RESULT
The given ARRAY:
Array
(
[two] => 2
[four] => 4
[six] => 6
[eight] => 8
[ten] => 10
)
5 ELEMENTS
The ARRAY to insert:
Array
(
[twelve] => 12
[fourteen] => 14
[sixteen] => 16
)
3 ELEMENTS
The result ARRAY:
Array
(
[two] => 2
[four] => 4
[six] => 6
[eight] => 8
[ten] => 10
[0] => Array
(
[twelve] => 12
[fourteen] => 14
[sixteen] => 16
)
)
6 ELEMENTS
EXERCISE
<?php
$empty_array = [];
$number = 5;
$str = "abc";
/* 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 ]
];
/* Error Conditions */
echo "Testing Edge Conditions:<br>";
/* Invalid Number of arguments */
var_dump( array_push($mixed_array[1],1,2) );
echo '<br><br>';
/* Empty Array as argument */
var_dump( array_push($empty_array, 2) );
/* Loop to test normal functionality
with different arrays inputs */
echo "<br><br>Testing with various array inputs:<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>Output after push is:<br>";
var_dump( array_push($sub_array, 22, "abc") );
$counter++;
echo '<br><br>';
}
/* Checking for return value and the
new array formed from push operation */
echo "<br><br>Checking for return value
and the new array formed from push operation:<br>";
var_dump( array_push($mixed_array[2], 22, 33, "44") );
echo '<br><br>';
var_dump( $mixed_array[2] );
?>
EXERCISE
<?php
/*
* Test basic functionality of array_push
* with indexed and associative arrays
*/
echo "Testing array_push():<br>basic functionality:<br><br>";
$array = array ('zero', 'one', 'two');
$var1 = 'three';
$var2 = 'four';
echo "<br>Push values onto an indexed array:<br>";
var_dump(array_push($array, $var1, $var2));
echo '<br><br>';
var_dump($array);
$array_assoc = array ('one' => 'un', 'two' => 'deux');
echo "<br><br>Push values onto an associative array:<br>";
var_dump(array_push($array_assoc, $var1, $var2));
echo '<br><br>';
var_dump($array_assoc);
?>
EXERCISE
<?php
$array = [1,2,3];
$values = [];
var_dump( array_push($array) );
echo '<br><br>';
var_dump( array_push($array, ...$values) );
echo '<br><br>';
var_dump( $array );
?>
EXERCISE
<?php
/*
* Test array_push when passed:
* 1. an array as $var arg
* 2. as sub-array as $stack arg
*/
echo "Testing array_push():<br>usage variations<br><br>";
echo "Pass array as \$var argument:<br>";
$array = array(1, 2, 3);
$sub_array = array('one', 'two');
var_dump(array_push($array, $sub_array));
echo '<br><br>';
var_dump($array);
echo "<br><br>Pass sub-array as \$stack argument:<br>";
var_dump(array_push($array[3], 'a'));
echo '<br><br>';
var_dump($array);
?>
EXERCISE
<?php
/*
* Check the position of the internal
* array pointer after calling array_push()
*/
echo "Testing array_push():<br>usage variations<br><br>";
$stack = array ('one' => 'un', 'two' => 'deux');
$var0 = 'zero';
echo "<br> Call array_push()<br>";
var_dump($result = array_push($stack, $var0));
echo "<br><br>Position of Internal Pointer in Original Array:<br>";
echo key($stack) . " => " . current ($stack);
?>
EXERCISE
<?php
/*
* Pass array_push arrays where
* the keys are different data types.
*/
echo "Testing array_push():<br>usage variations<br><br>";
// Initialise function arguments not being substituted
$var = 'value';
//get an unset variable
$unset_var = 10;
unset ($unset_var);
// heredoc string
$heredoc = <<<EOT
hello world
EOT;
// arrays of different data types as keys
// to be passed to $stack argument
$inputs = array(
// int data
/*1*/ 'int' => array(
0 => 'zero',
1 => 'one',
12345 => 'positive',
-2345 => 'negative',
),
// float data
/*2*/ 'float' => array(
10.5 => 'positive',
-10.5 => 'negative',
.5 => 'half',
),
'extreme floats' => array(
12.3456789000e10 => 'large',
12.3456789000E-10 => 'small',
),
// null data
/*3*/ 'null uppercase' => array(
NULL => 'null 1',
),
'null lowercase' => array(
null => 'null 2',
),
// boolean data
/*4*/ 'bool lowercase' => array(
true => 'lowert',
false => 'lowerf',
),
'bool uppercase' => array(
TRUE => 'uppert',
FALSE => 'upperf',
),
// empty data
/*5*/ 'empty double quotes' => array(
"" => 'emptyd',
),
'empty single quotes' => array(
'' => 'emptys',
),
// string data
/*6*/ 'string' => array(
"stringd" => 'stringd',
'strings' => 'strings',
$heredoc => 'stringh',
),
// undefined data
/*8*/ 'undefined' => array(
@$undefined_var => 'undefined',
),
// unset data
/*9*/ 'unset' => array(
@$unset_var => 'unset',
),
);
// loop through each sub-array of $inputs to
// check the behavior of array_push()
$iterator = 1;
foreach($inputs as $key => $input) {
echo "<br>Iteration $iterator : $key data<br>";
echo "Before : ";
var_dump(count($input));
echo "<br><br>After : ";
var_dump( array_push($input, $var) );
$iterator++;
};
?>