PHP Array_splice () junction Array usage Example _php tutorial

Source: Internet
Author: User
Tags pear
To join an array in PHP, we have a ready-to-use function, which is the Array_splice () function, which removes all elements from offset to end of offset+length in the array and returns the deleted element as an array. The Array_splice () syntax format is:

Array array_splice (array array, int Offset[,length[,array replacement])

When offset is positive, the join starts at offset from the beginning of the array, and when offset is negative, the join starts at the offset position from the end of the array. If the optional length parameter is omitted, all elements from the offset position to the end of the array are deleted. If length is given and positive, the join ends at offset + Leng th at the beginning of the array. Conversely, if length is given and is negative, the union ends at the position of Count (Input_array)-length from the beginning of the array.

To see an example of a standard junction array, PHP code:

1

2$fruits = Array ("Apple", "Banana", "Orange", "Pear", "Grape", "Lemon", "watermelon");

3$subset = Array_splice ($fruits, 4);

4print_r ($fruits);

5print_r ($subset);

The 6//output results are:

7//Array ([0] = Apple [1] = Banana [2] = Orange [3] = Pear)

8//Array ([0] = Grape [1] = Lemon [2] = watermelon)

9?>

We can also use the optional parameter replacement to specify an array that supersedes the target part. Take a look at the following example:

1

2$fruits = Array ("Apple", "Banana", "Orange", "Pear", "Grape", "Lemon", "watermelon");

3$subset = Array_splice ($fruits, 2,-1, Array ("Green Apple", "Red Apple"));

4print_r ($fruits);

5print_r ($subset);

6//Output Results:

7//Array ([0] = Apple [1] = Banana [2] + Green Apple [3] + Red Apple [4] = watermelon)

8//Array ([0] = Orange [1] = Pear [2] = = Grape [3] = Lemon)

9?>

http://www.bkjia.com/PHPjc/765440.html www.bkjia.com true http://www.bkjia.com/PHPjc/765440.html techarticle To join an array in PHP, we have a ready-made function that can be used, which is the Array_splice () function, which removes all elements from offset start to offset+length end in the array, and takes the array of ...

  • Related Article

    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.