Insert elements anywhere in the array, delete instances of specific elements, and insert element instances in the array
As follows:
$ary = array( array('t'=>1,'y'=>2), array('t'=>2,'y'=>9));$t = array_splice( $ary, 1,0,array(array('t'=>3,'y'=>10)));print_r($ary);
Console output:
$ary = array( array('t'=>1,'y'=>2), array('t'=>3,'y'=>10), array('t'=>2,'y'=>9));
The array_splice method is briefly introduced. Parameter 1 is the array to be operated, parameter 2 is the index value of the operating element, parameter 3 is the length, and parameter 4 is the element to be replaced. The effect of this method is to delete the coherent element of parameter 3 with parameter 2 as the starting position in parameter 1 array, and then add it with parameter 4.
If the length is 0, the effect is equivalent to inserting the specified element at the specified index value.
If the length is 1, the effect is equivalent to removing the index value element.
$ary = array( array('t'=>1,'y'=>2),);
Delete special elements in an array
$arr1 = array(1,3, 5,7,8);$key = array_search(3, $arr1);if ($key !== false){ array_splice($arr1, $key, 1);}var_dump($arr1);
Output: array (1, 5, 7, 8 );
Array_slice (array, start, length, preserve)
Start from the start element of the array and return the remaining elements in the array.
$a=array("red","green","blue","yellow","brown");print_r(array_slice($a,2));
Output array ("blue", "yellow", "brown ")
Array_push
Array_push -- push one or more units to the end of the array (into the stack)
Description
Int array_push (array & array, mixed var [, mixed...])
Array_push () treats array as a stack and pushes the passed variables to the end of array. The length of array increases according to the number of variables in the stack.
The above array inserts an element anywhere. The example of deleting a specific element is all the content shared by Alibaba Cloud xiaobian. I hope you can give us a reference and support for helping customers.