Home >php教程 >php手册 >Insert value anywhere in php array

Insert value anywhere in php array

WBOY
WBOYOriginal
2016-10-19 10:19:321320browse

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 array_splice above is just pig’s feet,

array_push

array_push -- Push one or more cells to the end of the array (push)

Instructions

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

array_push() treats array as a stack and pushes the passed variables into the end of array. The length of array will be increased 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