Home > Backend Development > PHP Problem > How to add data to an array in php

How to add data to an array in php

青灯夜游
Release: 2023-03-15 15:36:02
Original
6361 people have browsed it

php method to add data to an array: 1. Use the "array_unshift(array, data value 1, data value 2...)" statement; 2. Use "array_push(array, value 1, value 2. ..)" statement; 3. Use the "array_splice(array, starting position, 0, value)" statement.

How to add data to an array in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

php adds to the array Data

Method 1: array_unshift() function

##array_unshift($array,$value1,$value2...)The function can insert one or more new elements (key values) at the beginning of the array.

<?php
$arr=array(10,12,20);
array_unshift($arr,8,"9");
var_dump($arr);
?>
Copy after login

How to add data to an array in php

Method 2: array_push() function

array_push($array,$value1,$value2.. .)The function can insert one or more elements (key values) at the end of the array.

<?php
$arr=array(10,12,20);
array_push($arr,8,"9",3.14);
var_dump($arr);
?>
Copy after login

How to add data to an array in php

Method 3: array_splice() function

array_splice($array,$start,$length,$ value) function is a powerful function that can be used to delete array elements, replace array elements, and also insert array elements (just set the parameter $length to 0).

When

$length=0, then the parameter $start can specify the position (subscript) to start inserting, and the parameter $value can specify the insertion value (if If it is multiple values, it needs to be set as an array).

<?php
header("Content-type:text/html;charset=utf-8");
$arr1=array(10,12,20);
array_splice($arr1,0,0,"1");
var_dump($arr1);

$arr2=array(10,12,20);
array_splice($arr2,0,0,array("1",25,"3"));
var_dump($arr2);
?>
Copy after login

How to add data to an array in php

Method 4: array_pad() function

array_pad($array,$size,$value)The function can insert a certain key value $value into the array $array, thereby filling the array to the specified length $size. (The $size parameter can be understood as the final number of elements in the array, that is, the length of the array after the insertion operation).

<?php
$arr=array(10,12,20);
$result =array_pad($arr,5,1);
var_dump($result);
?>
Copy after login

It can be seen from

array_pad($arr,5,1) that the value of $size is 5, then there are a total of 5 elements in the array after the insertion operation; and the original There are 3 elements, so only 2 elements need to be inserted. And because the inserted value $value is 1, you only need to insert 2 elements with the value "1". Therefore, the output result is:

How to add data to an array in php

Recommended learning: "

PHP Video Tutorial"

The above is the detailed content of How to add data to an array in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template