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
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
- $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>"; ?>
Output:
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>"; ?>
Output:
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>"; ?>
Output:
The above is the detailed content of PHP array_push(). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics





PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
