Heim > Backend-Entwicklung > PHP-Tutorial > PHP array_push()

PHP array_push()

王林
Freigeben: 2024-08-29 12:45:37
Original
878 Leute haben es durchsucht

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

Syntax and Parameters of PHP array_push() are:

array_push($array1, $value1, $value2, $value3, …..)
Nach dem Login kopieren

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

  • $array1 Parameter of the array_push() function: The $array1 parameter of the array_push() function is actually the original array which is actually specified or operated. It is the main array that contains all the array elements which are previously defined.
  • List of values (Multiple Value Parameters ): The List of values are the multiple parameters of the array_push() function of the PHP Programming Language. This parameter is the bunch of list of elements that are actually separated with the help of commas and those elements which are separated will be pushed into some specific array. Let those array be $value1, $value2, $Value3, $Value4 and so on.
  • Return value of the array_push() function: The array_push() function of PHP Programming Language will only return the modified array just by adding/pushing some elements with the help of the parameter values which are referred to inside of the array_push() function. These elements which are added will be placed at the last index values of the array/arrays based on our requirement.

How array_push() function work in PHP?

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.

Example #1

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


tag is used for a horizontal line. After that, an array variable is created with some string array index values/elements with the help of array() function but here key are not defined. Then original array elements will be printed with the help of the “print_r()” function. Then some value variables are created and stored some string values inside of it. Here six string variables with values are created. Then array_push() function is used with the original variable and all the six string variables passed to it. This will push all the mentioned elements into the specific array. Then print_r($array1) function will print the array with all the extra elements.

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>";
?>
Nach dem Login kopieren

Output:

PHP array_push()

Example #2

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>";
?>
Nach dem Login kopieren

Output:

PHP array_push()

Example #3

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>";
?>
Nach dem Login kopieren

Output:

PHP array_push()

Das obige ist der detaillierte Inhalt vonPHP array_push(). Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Verwandte Etiketten:
php
Quelle:php
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage