Home > Backend Development > PHP Problem > How to use array_unshift function in php

How to use array_unshift function in php

藏色散人
Release: 2023-02-22 17:58:01
Original
3149 people have browsed it

Usage of array_unshift function in php: [array_unshift(array,value1)]. The array_unshift function is used to insert new elements into the array. The new elements will be inserted at the beginning of the array.

How to use array_unshift function in php

How to use the php array_unshift function?

(Recommended tutorial: php video tutorial)

Function: Used to insert new elements into the array. The values ​​of the new array will be inserted at the beginning of the array.

Syntax:

array_unshift(array,value1,value2,value3...)
Copy after login

Parameters:

array Required. Specifies an array.

value1 Required. Specifies the value to be inserted.

value2 Optional. Specifies the value to be inserted.

value3 Optional. Specifies the value to be inserted.

Explanation:

The added elements are added as a whole. The order of these elements in the array is the same as the order in the parameters. This function returns the number of elements in the array. You can insert one or more values. Numeric key names will start at 0 and increase by 1. String key names will remain unchanged.

php array_unshift() function usage example 1

<?php
$a=array("a"=>"西门","b"=>"灭绝");
print_r(array_unshift($a,"无忌"));
echo "<br>";
print_r($a);
?>
Copy after login

Output:

3
Array ( [0] => 无忌 [a] => 西门 [b] => 灭绝 )
Copy after login

php array_unshift() function usage example 2

<?php
$a=array("a"=>"欧阳克","b"=>"西门");
print_r(array_unshift($a,"peter_zhu")); //返回数组长度
echo "<br>";
print_r($a); //输出新的数组
?>
Copy after login

Output:

3
Array ( [0] => peter_zhu [a] => 欧阳克 [b] => 西门 )
Copy after login

The above is the detailed content of How to use array_unshift function in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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