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学习者快速成长!