PHP array_push() Function

PHP array_push() Function

The array_push() function in PHP pushes one or more elements onto the end of the array. This function increases the length of the array by the number of variables pushed.

Syntax

array_push ( array &$array, mixed& value [, mixed $... ] )

Parameter

array(required)- This parameter represents the input array.

value(required)- This parameter specifies the value to push in the array.

…(optional)- It represents the values to push onto the end of the array.

Return

This function returns the new number of elements in the array.

Example 1

 

Output

Input array: 
 Array
 (
     [0] => Cheese Cake
     [1] => Choco Lava
     [2] => Pie cut
 )
 After pushing 2 elements. 
 New Array: Array
 (
     [0] => Cheese Cake
     [1] => Choco Lava
     [2] => Pie cut
     [3] => Rapsberry Pie
     [4] => Vani 
 ) 

Example 2

"Reema", 2=>"Sukla",3=>"Varun", 4=>"Amar");
 echo("Input array: \n");
 print_r($array);
 //pushing 2 integer values 
 array_push($array, 45,67,89);
 echo("\nAfter pushing 2 elements.\nNew Array: "); 
 print_r($array);
 ?> 

Output

Input array: 
 Array
 (
     [1] => Reema
     [2] => Sukla
     [3] => Varun
     [4] => Amar
 )
 After pushing 2 elements. 
 New Array: Array
 (
     [1] => Reema
     [2] => Sukla
     [3] => Varun
     [4] => Amar
     [5] => 45
     [6] => 67
     [7] => 89 
 ) 

Example 3

"Reema", 2=>"Sukla",3=>"Varun", 4=>"Amar");
 echo("Input array: \n");
 print_r($array);
 //pushing the array with null 
 array_push($array, null);
 echo("\nAfter pushing null element.\nNew Array: "); 
 print_r($array);
 ?> 

Output

Input array: 
 Array
 (
     [1] => Reema
     [2] => Sukla
     [3] => Varun
     [4] => Amar
 ) 
 After pushing 2 elements.
 New Array: Array
 (
     [1] => Reema
     [2] => Sukla
     [3] => Varun 
     [4] => Amar
     [5] => 
 ) 

Related Topics

PHP usort() Function

PHP usort() Function The usort() function in PHP sorts  array by values using a user-defined comparison function. Syntax usort ( array &$array , callable $value_compare_func ) array(required)- This parameter represents the input array. value_compare_func(required)- This parameter represents a string that describe a callable comparison function. The comparison...

1 minute read.

PHP floatval() Function

PHP floatval() Function The floatval() function in PHP is used to give the float value of a variable. Syntax floatval ( mixed $var ) Parameter var(required)- This parameter represents the name of the variable that is to be converted. Return This function...

1 minute read.

PHP str_shuffle() Function

PHP str_shuffle() Function The str_shuffle() function in PHP randomly shuffles all the characters of the specified string. Syntax str_shuffle ( string $str ) Parameter str(required)- This parameter represents the input string to shuffle. Return This function returns the shuffled string. Example 1 ...

1 minute read.

PHP crypt() Function

PHP crypt() Function The crypt () function in PHP returns a hashed string using DES, Blowfish, or MD5 algorithms. Some constants of crypt() function are as follows: [CRYPT_STD_DES][CRYPT_EXT_DES][CRYPT_MD5][CRYPT_BLOWFISH][CRYPT_SHA_256][CRYPT_SHA_512] etc. Syntax crypt ( string $str [, string $salt ] )  Parameter str(required)- This parameter represents the string to be hashed salt(optional)- This parameter...

2 minutes read.

PHP in_array() Function

PHP in_array() Function The in_array() function in PHP checks if the value exists in the specified array or not. Syntax in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] )  Parameter needle(required)- This parameter represents the searched value. haystack(required)- This parameter represents the input array. strict(optional)- This parameter...

1 minute read.

PHP array_uintersect() Function

PHP array_uintersect() Function The array_uintersect() function in PHP calculates the intersection of arrays by comparing the data by a callback function. It returns an array containing all the values of the first input array that are...

2 minutes read.

PHP Environment Setup

To install PHP, We have to installed AMP(Apache, MySQL and PHP) software. There are various AMP software available in the market. XAMPP (Cross-Platform, Apache, MySQL, PHP and Perl) for windows. ...

2 minutes read.

PHP Date and Time

DATE: The PHP date() function formats a date or time. The timestamp is used to format a more readable date and time into a PHP date () function. Syntax: date(format,timestamp) Simple Date: Characters that...

1 minute read.

PHP is_string() Function

PHP is_string() Function The is_ string() function in PHP finds whether a variable is a string or not. It returns a Boolean value TRUE if the parameter var is string, else it returns FALSE. Syntax is_string ( mixed $var ) Parameter var(required)- This parameter represents...

1 minute read.

PHP array_udiff_assoc() Function

PHP array_udiff_assoc() Function The array_udiff_assoc () function in PHP computes the difference between the specified arrays with additional index check and compares the data by a callback function. It returns all the values from array1 that are not...

1 minute read.

PHP settype() Function

PHP settype() Function The settype() function in PHP is used to set the type of variable var to type. Syntax settype ( mixed &$var , string $type ) Parameter var(required)- This parameter represents the variable being converted. type(required)- This parameter represents the type of variable that is needed.  The possible values of type are...

1 minute read.

PHP array_reduce() Function

PHP array_reduce() Function The array_reduce() function in PHP reduces the array to a single value using by applying a callback function to the elements of the array. Syntax array_reduce ( array $array , callable $callback [, mixed $initial = NULL ] ) Parameter array(required)- This parameter represents the input array. callback (required)- This...

1 minute read.

PHP unset() Function

PHP unset() Function The unset() function in PHP is used to unset a given variable. This function destroys the specified variables. If this function is called inside any user-defined function, then it unsets the value associated with...

1 minute read.

PHP htmlentities() Function

PHP htmlentities() Function The htmlentities() function in PHP converts all characters to HTML entities. Syntax htmlentities(string,flags,character-set,double_encode);   Parameter string(required)- This parameter specifies the string to convert. flags(optional)- This parameter specifies how to handle quotes, invalid encoding and the used document type. The...

1 minute read.

PHP array_key_first() function

The array_key_first () function in PHP gets the first key of an array if the specified array is not empty. Syntax array_key_first(array;$array) Parameter array(required)- This parameter signifies the input array. Return This function returns the first key of the specified...

1 minute read.

PHP prev() Function

PHP prev() Function The prev() function in PHP rewinds or moves back the internal array pointer one place backward and returns the prev array value. It behaves like next() function, except that this function rewinds the...

1 minute read.

PHP strnatcasecmp() Function

PHP strnatcasecmp() Function The strnatcasecmp() function in PHP compares two strings using a "natural" algorithm. It is a case-insensitive function. Syntax strnatcasecmp ( string $str1 , string $str2 )  Parameter str1(required)- This parameter represents the first string. str2(required)- This parameter represents the second string. Return This function returns a value less...

1 minute read.

PHP Constant

PHP constant is an identifier(name) for a simple value. The value does not change during the script. It starts with a letter or underscore not $ sign. The define()function is...

1 minute read.

PHP array_change_key_case() Function

PHP array_change_key_case() Function The array_change_key_case() function in PHP is used to change the case of all keys present in the specified array. It returns an array with all keys in the specified array to lowercase or...

1 minute read.

PHP $ and $$ Variables

PHP $var variable is a normal variable that stores any value. PHP $$var variable is a reference variable that stores the value of$var inside it. Let us take an example. Example 1 <?php $x =...

1 minute read.