Home  >  Article  >  Backend Development  >  Detailed explanation of php array_unshift() function (example)

Detailed explanation of php array_unshift() function (example)

王林
王林Original
2020-08-14 17:24:593419browse

Detailed explanation of php array_unshift() function (example)

array_unshift() function is used to insert new elements into the array and return the number of elements in the new array. The values ​​of the new array will be inserted at the beginning of the array.

(Recommended tutorial: php graphic tutorial)

Tip: You can insert one or more values. Numeric key names will start at 0 and increase by 1. String key names will remain unchanged.

Function syntax:

array_unshift(array,value1,value2,value3...)

Parameter description:

  • 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.

(Learning video recommendation: php video tutorial)

Example:

<?php
$a=array("a"=>"red","b"=>"green");
array_unshift($a,"blue");
print_r($a);
?>

Run result:

Array ( [0] => blue [a] => red [b] => green )

The above is the detailed content of Detailed explanation of php array_unshift() function (example). For more information, please follow other related articles on the PHP Chinese website!

Statement:
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