Home > Article > Backend Development > How to put several data into an array in php
4 methods: 1. Directly assign values to array elements without specifying key names, the syntax is "$array name[]=value 1;$array name[]=value 2;..."; 2. Use array_unshift(), the syntax is "array_unshift(array, value 1, value 2...)"; 3. Use array_push(), the syntax is "array_push(array, value 1, value 2...)"; 3. Use " array_splice(array, position, 0, array(value 1, value 2...))" statement.
The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer
php puts several data into the array, that is Insert multiple data into the array. Here are several insertion methods.
Method 1: Directly assign values to array elements without specifying key names
We can pass the array variable name in the form of "$ [Subscript] = value;
" format to initialize the array
When we do not specify a specific index value (subscript) within square brackets, the default is a numeric index, and the index value defaults from 0 Start to increase gradually.
<?php header("content-type:text/html;charset=utf-8"); $arr=array(10,12,20); var_dump($arr); $arr[]="hello"; $arr[]=8; $arr[]=9; echo "插入多个数据后:"; var_dump($arr); ?>
Method 2: Use the array_unshift() function to insert multiple data at the beginning of the array
array_unshift($array,$value1,$value2...)
The function can insert one or more new elements (key values) at the beginning of the array.
<?php header('content-type:text/html;charset=utf-8'); $arr=array(10,12,20); var_dump($arr); array_unshift($arr,8,"9"); echo "插入多个数据后:"; var_dump($arr); ?>
Method 3: Use the array_push() function to insert multiple data at the end of the array
array_push($array,$value1,$value2...)
The function can insert one or more elements (key values) at the end of the array.
<?php header("content-type:text/html;charset=utf-8"); $arr=array(10,12,20); var_dump($arr); array_push($arr,8,"9",3.14); echo "插入多个数据后:"; var_dump($arr); ?>
Method 4: Use array_splice() function to insert multiple data (any position) in the array
array_splice($array,$start,$length,$value)
The function is a powerful function that can be used to delete array elements, replace array elements, and also insert array elements (only You need to 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 The insertion value can be specified (if there are multiple values, it needs to be set as an array).
$start
value is 0, then insert
$start
value at the beginning. "Array length value", then insert
$start
value at the end >0 and in the middle
Note: The array_splice() function will change the original array
<?php header("content-type:text/html;charset=utf-8"); $arr1=array(10,12,20); var_dump($arr1); echo "插入多个数据后:"; array_splice($arr1,0,0,array("1","hello","3")); var_dump($arr1); $arr2=array(10,12,20); array_splice($arr2,1,0,array("1",25,"3")); var_dump($arr2); $arr3=array(10,12,20); array_splice($arr3,count($arr3),0,array("1",25,"3")); var_dump($arr3); ?>
Recommended study: "PHP Video Tutorial"
The above is the detailed content of How to put several data into an array in php. For more information, please follow other related articles on the PHP Chinese website!