php拆分数组的二个函数(array_slice()、array_splice())

WBOY
풀어 주다: 2016-07-25 09:04:34
원래의
1527명이 탐색했습니다.
  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. ?>
复制代码

上例将输出: Array ( [0] => c [1] => d ) Array ( [2] => c [3] => d )

2.array_splice()

携带三个参数,同上,作用是删除从offset开始长度为length的子数组。

例子:

  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. ?>
复制代码


원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!