Home >类库下载 >PHP类库 >Insert value anywhere in php array

Insert value anywhere in php array

高洛峰
高洛峰Original
2016-10-19 10:04:101155browse

array_splice()

$arr = array('A', 'B', 'C');

$arr2 = 'abc';
$t = array_splice($arr, 1, 0, $arr2);

print_r($arr);

Console output:

Array ( [0] => 'A' [1] => 'abc' [2] => 'B' [3] => 'C' );

A brief introduction to the array_splice method. The first parameter is the array being operated on, the second parameter is the index value of the operating element, the third parameter is the length, and the fourth parameter is the element to be replaced.

The effect of this method is to delete the consecutive elements with 1 as the starting position and length 0 in $arr, and then fill them with $arr2.

tips: If the length is 0, the effect is equivalent to inserting the specified element at the specified index value.

------------------------------------------------ ------------Gorgeous dividing lineoo00o0ooo0oo. . . --------------------------______________________________

The above array_splice is just a pig's foot,

array_push

array_push -- Push one or more units into the array End (push)

Explanation

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

array_push() treats array as a stack and pushes the incoming variables into array the end of. The length of array will increase according to the number of variables pushed onto the stack. The same effect as the following:


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

Related articles

See more