Home >Backend Development >PHP Tutorial >PHP implements the method of adding elements to the head and tail of an array

PHP implements the method of adding elements to the head and tail of an array

墨辰丷
墨辰丷Original
2018-05-24 09:55:213395browse

For numeric index arrays, add elements to the tail of the array through the array_push() function, and add elements to the head of the array_unshift. Friends in need can refer to the following

For numeric index arrays, use array_push( ) function adds elements to an array.
The array_push() function treats the array as a stack and pushes the incoming variables to the end of the array. The length of the array will increase as the number of variables pushed onto the stack increases, and the total number of new units in the array is returned.

Add elements at the end

The syntax format is as follows:

int array_push (array &$array, mixed $var [, mixed $...] )

The parameter array is the specified array, and the parameter $var is the value pushed into the array.

The following is the array_push() function to add elements to the end of the array. The specific example code is as follows:

<?php
header("Content-Type:text/html; charset=utf-8");
$array_push = array("PHP中文网","百度一下");//定义数组
array_push($array_push,"搜狗浏览器","火狐浏览器");//添加元素
print_r($array_push);// 输出数组结果
?>

Output results For:

PHP implements the method of adding elements to the head and tail of an array

Another easier way to add array elements, for numeric subscripted arrays:

$names[] = &#39;ruby&#39;;

The function is similar to array_push, but only one can be added at a time. For associative arrays, you can add key in square brackets

$info[&#39;height&#39;] = 1.7;

Reference code

$names[] = &#39;lucy&#39;;
$names[] = &#39;lily&#39;;
// 等同于
array_push($names, &#39;lucy&#39;, &#39;lily&#39;);

array_unshift adds elements to the head

The principle of array_push is similar, but the direction is different.

The syntax format is as follows:

int array_unshift ( array &$array , mixed $var [, mixed $... ] )

Let’s introduce it to you directly through examples. array_unshift() function, the specific code is as follows:

<?php
header("Content-Type:text/html; charset=utf-8");
$names = [&#39;andy&#39;, &#39;tom&#39;, &#39;jack&#39;];
array_unshift($names, &#39;joe&#39;, &#39;hank&#39;);
print_r($names);
?>

The output result is:

PHP implements the method of adding elements to the head and tail of an array

The above is The entire content of this article is hoped to be helpful to everyone's study.


Related recommendations:

PHP Two-dimensional array deduplication or statistics based on a certain field

php http request class

PHP mkdir new folder permission problem

The above is the detailed content of PHP implements the method of adding elements to the head and tail of an array. 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