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!

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

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

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1
Powerful PHP integrated development environment
