PHP array Segmentation and array splicing

Source: Internet
Author: User
    1. $fruits = Array ("Apple", "Banana", "Orange", "Pear", "Grape", "Lemon", "watermelon");

    2. $subset = Array_slice ($fruits, 3);
    3. Print_r ($subset);

    4. Output

    5. Array ([0] = Pear [1] = Grape [2] = = Lemon [3] = = Watermelon)
    6. ?>
Copy Code

Then, use the following negative length:

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

    2. $subset = Array_slice ($fruits, 2,-2);
    3. Print_r ($subset);

    4. Output

    5. Array ([0] = Orange [1] = Pear [2] = Grape)
    6. ?>
Copy Code

Junction Array Array_splice ()

The Array_splice () function deletes all elements in the array from offset to end of Offset+length and returns the deleted element as an array. When the form is: array array_splice (array array, int Offset[,length[,array replacement]) offset is positive, the join starts at offset from the beginning of the array, When offset is negative, the join starts from the offset position at 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.

Example:

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

    2. $subset = Array_splice ($fruits, 4);

    3. Print_r ($fruits);

    4. Print_r ($subset);

    5. Output

    6. Array ([0] = Apple [1] = Banana [2] = Orange [3] = Pear)
    7. Array ([0] = Grape [1] = Lemon [2] = watermelon)
    8. ?>
Copy Code

You can use the optional parameter replacement to specify an array that supersedes the target part.

Example:

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

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

    3. Print_r ($fruits);

    4. Print_r ($subset);

    5. Output

    6. Array ([0] = Apple [1] = Banana [2] = = Green Apple [3] + Red Apple [4] = watermelon)
    7. Array ([0] = Orange [1] = Pear [2] = Grape [3] = = Lemon)
    8. ?>

Copy Code
  • 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.