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 strtolower() Function

The strtolower() function in PHP is used to convert a string to lowercase. This function is binary-safe. The related functions are as follows: strtoupper()lcfirst()ucfirst()ucwords() Syntax strtolower ( string $string ) Parameter string(required)- This parameter represents the input string. Return This function returns a string with all alphabetic...

1 minute read.

PHP number_format() Function

PHP number_format() Function The number_format() function in PHP formats a number with grouped thousands. This function accepts one, two, or four parameters (not three): Syntax number_format ( float $number [, int $decimals ] )             or number_format ( float $number , int $decimals  , string $dec_point, string$thousands_sep ) Parameter number(required)- This parameter represents the number to be formatted. If...

1 minute read.

PHP quoted_printable_encode() Function

PHP quoted_printable_encode() Function The quoted_printable_encode() function in PHP converts a 8 bit string to a quoted-printable string. This function is similar to imap_8bit(), except this one does not require the IMAP module to work. Syntax quoted_printable_encode ( string $str ) Parameter str(required)- This parameter represents...

1 minute read.

PHP sha1_file() Function

PHP sha1_file() Function The sha1_file() in PHP calculates the sha1 hash of a file and returns the hash (40-character hexadecimal number). Syntax sha1_file ( string $filename [, bool $raw_output] )  Parameter filename(required)- This parameter represents the filename of the file to hash. raw_output(optional)- This...

1 minute read.

PHP chunk_split() Function

PHP chunk_split() Function The chunk_split() function in PHP splits a string into a series of smaller parts. Syntax chunk_split(string,length,end) Parameter string(required)- It specifies the string to split. length(optional)- This parameter represents a number that defines the...

1 minute read.

PHP is_scalar() Function

PHP is_scalar() Function The is_scalar() function in PHP finds whether a variable is a scalar or not. It returns a Boolean value TRUE if the parameter var is scalar, else it returns FALSE. Scalar variables are those containing an integer, float, string or boolean. This...

1 minute read.

PHP is_int() Function

The PHP is_int() function in PHP is used to  find whether a variable is an integer or not. It returns a Boolean value true if the parameter var is integer else it...

1 minute read.

PHP empty() Function

PHP empty() Function The empty() function in PHP determines whether the specified variable is empty or not. The specified variable is considered empty if it does not exist or if its value is equal to FALSE. This function...

1 minute read.

PHP Include Function

PHP Include( ) The PHP include() statement holds all the code/text that liesin the defined file and paste it into the file that uses the "include" statement. With the help of include...

3 minutes read.

PHP strcspn() Function

PHP strcspn() Function The strcspn() function in PHP returns the number of characters (including whitespaces) found in a string before any part of the specified characters is found. It is a binary-safe function. Syntax strcspn ( string $subject , string $mask [, int $start [, int $length ]] ) Parameter subject(required)- This...

1 minute read.

PHP uasort() Function

PHP uasort() Function The uasort() function in PHP sorts an array with a user-defined comparison function and maintains the index association. Syntax uasort ( array &$array , callable $value_compare_func ) Parameter array(required)- This parameter represents the input array. value_compare_func(required)- This parameter represents a string that describe a...

1 minute read.

PHP wordwrap() Function

PHP wordwrap() Function The wordwrap() Function wraps a string to a given number of characters using a string break character. Syntax wordwrap ( string $str [, int $width [, string $break  [, bool $cut  ]]] ) Parameter str(required)- This parameter specifies the input string.       width(optional)- This parameter represents the number of...

1 minute read.

PHP setlocale() Function

PHP setlocale() Function The setlocale() function in PHP sets locale information(language, monetary, time and, other information specific for a geographical area.) Syntax setlocale ( int $category , array $locale ) Parameter category(required)- This parameter specified a named constant specifying the category of the functions affected...

1 minute read.

PHP is_long() Function

The PHP is_long () function in PHP is used to find whether a variable is an integer or not. It returns a Boolean value true if the parameter var is integer else it returns...

1 minute read.

PHP list() Function

PHP list() Function The list() function in PHP sorts an array by key while maintaining the keys. Syntax list ( mixed $var1 [, mixed $... ] ) Parameter var1(required)- This parameter accepts variables separated by spaces. …(optional)- This parameter accepts a list of variables separated...

1 minute read.

PHP strtoupper() Function

The strtoupper() function in PHP is used to convert a string to uppercase. This function is binary-safe. The related functions are as follows: strtolower()lcfirst()ucfirst()ucwords() Syntax strtoupper ( string $string ) Parameter string(required)- This parameter represents the input string. Return This function returns a string with all alphabetic...

1 minute read.

PHP Magic Constants

There are various predefine PHP Magic Constants. It starts with underscore(__) and ends with double underscore.  Magic Constant Description __LINE__ To check current line number of the file. __FILE__ To check full path and filename of...

1 minute read.

PHP array_rand() Function

PHP array_rand() Function The array_rand() function in PHP picks one or more random keys out of an array. Syntax array_rand ( array $array [, int $num = 1 ] ) Parameter array(required)- This parameter represents the input array. num(optional)- This parameter specifies how many entries should be picked....

1 minute read.

PHP str_pad() Function

PHP str_pad() Function The str_pad() function in PHP pads a string to the given length with another string. Syntax str_pad ( string $input , int $pad_length [, string $pad_string  [, int $pad_type ]] ) Parameter input(required)- This parameter signifies the input string. pad_length(required)- This parameter specifies the length of the new string. If this value...

2 minutes read.

PHP shuffle() Function

PHP shuffle() Function The shuffle() function in PHP shuffles an array in a random array. Syntax shuffle ( array &$array )  Parameter array(required)- This parameter represents the input array.  Return This parameter returns a Boolean value TRUE on success or FALSE on failure. Example 1 Output Original Array: Array ...

1 minute read.