Method: 1. Use the array_unshift() function to add an element to the beginning of the array, the syntax is "array_unshift(array, value)"; 2. Use the array_push() function to add an element to the end of the array, the syntax is " array_push(array,value)".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
Add elements at the head of the array
The array_unshift() function adds elements to the head of the array. All existing numeric keys are modified to reflect their new positions in the array, but associated keys are not affected. Its form is as follows:
array_unshift(array array,mixed variable[,mixed variable])
The following example adds two fruits in front of the $fruits array:
$fruits = array("apple","banana"); array_unshift($fruits,"orange","pear") // $fruits = array("orange","pear","apple","banana");
Add elements at the end of the array
array_push The () function adds (push) one or more elements to the end of the first argument's array and returns the length of the new array.
The return value of the array_push() function is int type, which is the number of elements in the array after pushing the data. You can pass multiple variables as parameters to this function and push multiple variables into the array at the same time. Its form is:
array_push(array,value1,value2...)
The following example adds two more fruits to the $fruits array:
view sourceprint? $fruits = array("apple","banana"); array_push($fruits,"orange","pear") //$fruits = array("apple","banana","orange","pear")
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to add an element to a php array. 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

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Zend Studio 13.0.1
Powerful PHP integrated development environment

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
