Insert an element anywhere in the PHP array to delete instance details of a specific element

Source: Internet
Author: User
The following section describes how to insert elements in multiple groups and delete instances of specific elements. I think this is quite good. now I will share it with you and give you a reference. Let's take a look at the following small series to bring you an example of inserting elements from any location in a number of groups and deleting specific elements. I think this is quite good. now I will share it with you and give you a reference. Let's take a look at it with Xiaobian.

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 is the content of the instance details for deleting a specific element by inserting an element anywhere in the array. For more information, see PHP Chinese website (www.php1.cn )!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.