Heim > Artikel > Backend-Entwicklung > PHP array_push()
The array_push() function of the PHP Programming Language is actually an in built function which helps in pushing the new elements into a specific array/arrays based on our requirement. We can push one element or many elements into the specific array based on our requirements and these array elements will be inserted at the last section/index value positions. Due to the usage of array_push() function, the length of the specific array will be increased/incremented according to the number of elements that are pushed into the specific array.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Syntax and Parameters of PHP array_push() are:
array_push($array1, $value1, $value2, $value3, …..)
Explanation of the parameters of the array_push() function:
There will be more than one parameter available inside of the array_push() function of the PHP Programming Language. The number of parameters of the array_push() function is basically depends upon the number of elements which are actually pushing into the specific array. One can classify these parameters into two categories specifically. They are 1. $array1, 2. List of Values
The array_push() function of the PHP Programming Language basically works just by pushing some elements into the specific array. The array_push() function also works to push multi elements into the original array which is actually specified inside of the array_push() function. After making it work, the length of the array will be enhanced and it is based on the number of elements pushed into the array. If an array is having a key and value pair then the method will try to add the numeric key to the pushed value. This array_push() function of the PHP runs only on PHP 4, PHP 5 and on PHP 7 versions.
This is the example of illustrating the array_push() function with the help of the original array parameter and the value list parameters. Here at first inside of the PHP tags
Code:
<?php // PHP code which helps in illustrating the usage of array_push() function of PHP // The Input array echo "<hr>"; $array1 = array("ram", "krishna", "aakash"); echo "The array values which are present before pushing elements :: "; echo "<br>"; print_r($array1); echo "<hr>"; // elements to push $value1 = "pavan"; $value2 = "kumar"; $value3 = "sake"; $value4 = "anil"; $value5 = "maruthi"; $value6 = "raj"; echo "The array values which are present after using the pushing function :: "; echo "<br>"; // This is the array which is after the pushing of some new elements array_push($array1, $value1, $value2, $value3, $value4, $value5, $value6); print_r($array1); echo "<hr>"; ?>
Output:
This example is similar to example 1 but the difference here is that inside of the array() function, Key and value parameters are declared/mentioned( Key_value pairs are mentioned). Other than that everything is so similar to example 1. You can check the output of the program which is mentioned in the output section below to understand the array_push() function better and so easily.
Code:
<?php // PHP code which helps in illustrating the usage of array_push() function of PHP // The Input array echo "<hr>"; $array2 = array(1=>"rahim", 2=>"krishnaveni", 3=>"lion"); echo "The array values which are present before pushing elements :: "; echo "<br>"; print_r($array2); echo "<hr>"; // elements to push $valuea1 = "pavan"; $valuea2 = "sake"; $valuea3 = "kumar"; $valuea4 = "king"; $valuea5 = "queen"; $valuea6 = "birbal"; echo "The array values which are present after using the pushing function :: "; echo "<br>"; // This is the array which is after the pushing of some new elements array_push($array2, $valuea1, $valuea2, $valuea3, $valuea4, $valuea5, $valuea6); print_r($array2); echo "<hr>"; ?>
Output:
This example is a simple illustration of the array_push() function but here only some integer values are used as the array elements. Then four variables are created with some integer values to it. Then all those four variable values are pushed into the original array with the help of array_push() function. Other than this everything is similar to example 1 and 2. You can check the output below to understand the concept of array_push() better and so easily.
Code:
<?php // PHP code which helps in illustrating the usage of array_push() function of PHP // The Input array echo "<hr>"; $array2 = array(2, 42, 8); echo "The array values which are present before pushing elements :: "; echo "<br>"; print_r($array2); echo "<hr>"; // elements to push $valuea1 = 12; $valuea2 = 13; $valuea3 = 14; $valuea4 = 15; echo "The array values which are present after using the pushing function :: "; echo "<br>"; // This is the array which is after the pushing of some new elements array_push($array2, $valuea1, $valuea2, $valuea3, $valuea4); print_r($array2); echo "<hr>"; ?>
Output:
Das obige ist der detaillierte Inhalt vonPHP array_push(). Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!