PHP two functions for splitting a fractional group (Array_slice (), Array_splice ())

Source: Internet
Author: User
    1. $input = Array ("A", "B", "C", "D", "E");

    2. $output = Array_slice ($input, 2); Returns "C", "D", and "E"

    3. $output = Array_slice ($input,-2, 1); Returns "D"
    4. $output = Array_slice ($input, 0, 3); Returns "A", "B", and "C"

    5. Note the differences in the array keys

    6. Print_r (Array_slice ($input, 2,-1));
    7. Print_r (Array_slice ($input, 2,-1, true));
    8. ?>

Copy Code

The example above will output: Array ([0] = c[1] + d) Array ([2] = c[3] + D)

2.array_splice ()

Carry three parameters, ibid., to delete a subarray of length from offset.

Example:

  1. $input = Array ("Red", "green", "blue", "yellow");

  2. Array_splice ($input, 2);
  3. $input is now array ("Red", "green")

  4. $input = Array ("Red", "green", "blue", "yellow");

  5. Array_splice ($input, 1,-1);
  6. $input is now array ("Red", "yellow")

  7. $input = Array ("Red", "green", "blue", "yellow");

  8. Array_splice ($input, 1, Count ($input), "Orange");
  9. $input is now array ("Red", "orange")

  10. $input = Array ("Red", "green", "blue", "yellow");

  11. Array_splice ($input,-1, 1, Array ("Black", "maroon"));
  12. $input is now array ("Red", "green",
  13. "Blue", "Black", "maroon")

  14. $input = Array ("Red", "green", "blue", "yellow");

  15. Array_splice ($input, 3, 0, "purple");
  16. $input is now array ("Red", "green",
  17. "Blue", "purple", "yellow");
  18. ?>

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.