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 strval() function

PHP strval() function The strval() function in PHP is used to get the string value for the specified variable. This function cannot be used on arrays or objects, if applied then would return only the...

1 minute read.

PHP Tutorial

PHP Tutorial PHP Introduction PHP is a server-side scripting language which is used for web development. It stands for Hypertext Preprocessor. It is faster than another programming language. It supports multiple databases like MySQL, SQL, PostgreSQL and...

3 minutes read.

PHP array_keys() Function

PHP array_keys() Function The array_keys() function  in PHP is used to return all the keys, numeric and string from the given array. Syntax array_keys ( array $array ) or array_keys ( array $array , mixed $search_value [, bool $strict = FALSE ] )  Parameter array(required)- This parameter specifies the input array search_value(optional)- This parameter specifies a value, where...

1 minute read.

PHP quoted_printable_decode() Function

PHP quoted_printable_decode() Function The quote_printable_decode() function of PHP converts a quoted-printable string to an 8 bit string. It works similar to imap_qprint(), except this one does not require the IMAP module to work. Syntax quoted_printable_decode ( string $str ) Parameter str(required)- This parameter represents the...

1 minute read.

PHP is_real() Function

The PHP is_real () function in PHP is used to find whether the type of the specified variable is a float or not. It returns a Boolean value true if the parameter...

1 minute read.

PHP str_word_count() Function

PHP str_word_count() Function The str_word_count() function in PHP counts the number of words inside the string and returns the information about words used in a string. String str_word_count( string $string [, int $format [, string $charlist ]] ) Parameter string(required)- This parameter represents the input string. format(required)- This parameterspecifies the...

2 minutes read.

PHP array_key_last() function

The array_key_last () function in PHP gets the last key of an array if the specified array is not empty. Syntax array_key_last ( array $array )  Parameter array(required)- This parameter signifies the input array. Return This function returns the last key of 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 crc32() Function

PHP crc32() Function The crc32() function in PHP  Calculates the crc32 polynomial of a string. Syntax crc32( string $str ) Parameter str(required)- This parameter represents The string to be calculated Return This function returns the crc32 checksum of string as an integer. Example 1 ...

1 minute read.

PHP Echo And Print

In PHP, echo and print is used to get output data to the screen but there is some difference, between echo and print. PHP echo PHP echo is used to display the...

1 minute read.

PHP arsort() function

PHP arsort() function The arsort() function in PHP sorts an array in the reverse order and maintains the index association Syntax arsort ( array &$array [, int $sort_flags = SORT_REGULAR ] ) Parameter array(required)- This parameter represents the input array. sort_flags (optional)- This parameter is used to modify the behavior...

2 minutes read.

WordPress Theme

Writing our WordPress Theme is relatively simple. Requirements: Before we begin, we need to know about the following set of requirements. We need to have full knowledge of WordPress setup, which is either...

2 minutes read.

PHP var_export() Function

PHP var_export() Function The var_export() function in PHP is used to get the structured information for the specified variable. This function outputs or returns a parsable string representation of a variable. This function is similar to...

1 minute read.

PHP strip_tags() Function

PHP strip_tags() Function The strip_tags() function in PHP is sued to strip a string from HTML, XML, and PHP tags. It is a binary-safe function. Syntax strip_tags ( string $str [, string $allowable_tags ] ) Parameter str(required)- This parameter specifies the input string. allowable_tags(optional)- This parameter represents...

1 minute read.

PHP boolval() Function

PHP boolval() Function The boolval() function in PHP is used to give the Boolean value for a given variable. Syntax boolval ( mixed $var ) Parameter var(required)- This parameter represents the value that is converted to a boolean. It can be a...

1 minute read.

PHP similar_text() Function

PHP similar_text() Function The similar_text() function in PHP calculates the similarity between two strings. Syntax similar_text ( string $first , string $second [, float &$percent ] ) Parameter first(required): This parameter represents the first string to be compared. second(required): This parameter represents the second string to be compared. percent(optional): This...

1 minute read.

PHP foreach Loop

PHP foreach Loop with Example PHP foreach loop is used to loop through the associative arrays. Syntax: foreach($array as $key => $value) { //Statement } $array = associative array. $key = array key. &value =...

2 minutes 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 chr() Function

PHP chr() Function The chr() function in PHP generates a single byte string from a number and returns a one-character string containing the character as specified by the unsigned integer. Syntax chr(bytevalue);    Parameter bytevalue(required)- This...

1 minute read.

PHP ucfirst() Function

PHP ucfirst() Function The ucfirst() function in PHP converts the first character of the specified string to uppercase. Syntax ucfirst ( string $str ) Parameter str(required)- This parameter represents the input string. Return This function returns a string with the first character of the specified string capitalized if that...

1 minute read.