Home  >  Article  >  php教程  >  [PHP源码阅读]array_slice和array_splice函数

[PHP源码阅读]array_slice和array_splice函数

WBOY
WBOYOriginal
2016-06-10 15:03:061003browse

array_slice和array_splice函数是用在取出数组的一段切片,array_splice还有用新的切片替换原删除切片位置的功能。类似javascript中的Array.prototype.splice和Array.prototype.slice方法。

我在github上有对PHP源码更详细的注解。感兴趣的可以围观一下,给个star。PHP5.4源码注解。可以通过commit记录查看已添加的注解。

array_slice

array array_slice ( array $array , int $offset [, int $length = NULL [, bool $preserve_keys = false ]] )

返回数组中指定下标offset和长度length的子数组切片。

参数说明

设第一个参数数组的长度为num_in。

offset

如果offset是正数且小于length,则返回数组会从offset开始;如果offset大于length,则不操作,直接返回。如果offset是负数,则offset = num_in+offset,如果num_in+offset == 0,则将offset设为0。

length

如果length小于0,那么会将length转为num_in - offset + length;否则,如果offset+length > array_count,则length = num_in - offset。如果处理后length还是小于0,则直接返回。

preserve_keys

默认是false,默认不保留数字键值原顺序,设为true的话会保留数组原来的数字键值顺序。

使用实例

<span style="color: #000000;">php
</span><span style="color: #800080;">$input</span> = <span style="color: #0000ff;">array</span>("a", "b", "c", "d", "e"<span style="color: #000000;">);

</span><span style="color: #800080;">$output</span> = <span style="color: #008080;">array_slice</span>(<span style="color: #800080;">$input</span>, 2);      <span style="color: #008000;">//</span><span style="color: #008000;"> returns "c", "d", and "e"</span>
<span style="color: #800080;">$output</span> = <span style="color: #008080;">array_slice</span>(<span style="color: #800080;">$input</span>, -2, 1);  <span style="color: #008000;">//</span><span style="color: #008000;"> returns "d"</span>
<span style="color: #800080;">$output</span> = <span style="color: #008080;">array_slice</span>(<span style="color: #800080;">$input</span>, 0, 3);   <span style="color: #008000;">//</span><span style="color: #008000;"> returns "a", "b", and "c"</span>

<span style="color: #008080;">print_r</span>(<span style="color: #008080;">array_slice</span>(<span style="color: #800080;">$input</span>, 2, -1)); <span style="color: #008000;">//</span><span style="color: #008000;"> array(0 => 'c', 1 => 'd');</span>
<span style="color: #008080;">print_r</span>(<span style="color: #008080;">array_slice</span>(<span style="color: #800080;">$input</span>, 2, -1, <span style="color: #0000ff;">true</span>)); <span style="color: #008000;">//</span><span style="color: #008000;"> array(2 => 'c', 1 => 'd');</span>

运行步骤

处理参数:offset、length

移动指针到offset指向的位置

从offset开始,拷贝length个元素到返回数组

 

运行流程图如下

 

 

array_splice

array array_splice ( array &$input , int $offset [, int $length = 0 [, mixed $replacement = array() ]] )

删除input中从offset开始length个元素,如果有replacement参数的话用replacement数组替换删除掉的元素。

参数说明

 array_splice函数中的offset和length参数跟array_slice函数中的用法一样。

replacement

如果这个参数设置了,那么函数将使用replacement数组来替换。

如果offset和length指定了没有任何元素需要移除,那么replacement会被插入到offset的位置。

如果replacement只有一个元素,可以不用array()去包着它。

使用示例

<span style="color: #000000;">php
</span><span style="color: #800080;">$input</span> = <span style="color: #0000ff;">array</span>("red", "green", "blue", "yellow"<span style="color: #000000;">);
</span><span style="color: #008080;">array_splice</span>(<span style="color: #800080;">$input</span>, 2<span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;"> $input变为 array("red", "green")</span>

<span style="color: #800080;">$input</span> = <span style="color: #0000ff;">array</span>("red", "green", "blue", "yellow"<span style="color: #000000;">);
</span><span style="color: #008080;">array_splice</span>(<span style="color: #800080;">$input</span>, 1, -1<span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;"> $input变为 array("red", "yellow")</span>

<span style="color: #800080;">$input</span> = <span style="color: #0000ff;">array</span>("red", "green", "blue", "yellow"<span style="color: #000000;">);
</span><span style="color: #008080;">array_splice</span>(<span style="color: #800080;">$input</span>, 1, <span style="color: #008080;">count</span>(<span style="color: #800080;">$input</span>), "orange"<span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;"> $input变为 array("red", "orange")</span>

<span style="color: #800080;">$input</span> = <span style="color: #0000ff;">array</span>("red", "green", "blue", "yellow"<span style="color: #000000;">);
</span><span style="color: #008080;">array_splice</span>(<span style="color: #800080;">$input</span>, -1, 1, <span style="color: #0000ff;">array</span>("black", "maroon"<span style="color: #000000;">));
</span><span style="color: #008000;">//</span><span style="color: #008000;"> $input为 array("red", "green",
//          "blue", "black", "maroon")</span>

<span style="color: #800080;">$input</span> = <span style="color: #0000ff;">array</span>("red", "green", "blue", "yellow"<span style="color: #000000;">);
</span><span style="color: #008080;">array_splice</span>(<span style="color: #800080;">$input</span>, 3, 0, "purple"<span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;"> $input为 array("red", "green",
//          "blue", "purple", "yellow");</span>

 

源码解读

 在array_splice中,有这么一段代码:

    <span style="color: #008000;">/*</span><span style="color: #008000;"> Don't create the array of removed elements if it's not going
     * to be used; e.g. only removing and/or replacing elements </span><span style="color: #008000;">*/</span>
    <span style="color: #0000ff;">if</span> (return_value_used) { <span style="color: #008000;">//</span><span style="color: #008000;"> 如果有用到函数返回值则创建返回数组,否则不创建返回数组</span>
        <span style="color: #0000ff;">int</span> size =<span style="color: #000000;"> length;

        </span><span style="color: #008000;">/*</span><span style="color: #008000;"> Clamp the offset.. </span><span style="color: #008000;">*/</span>
        <span style="color: #0000ff;">if</span> (offset ><span style="color: #000000;"> num_in) {
            offset </span>=<span style="color: #000000;"> num_in;
        } </span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> (offset 0 && (offset = (num_in + offset)) 0<span style="color: #000000;">) {
            offset </span>= <span style="color: #800080;">0</span><span style="color: #000000;">;
        }

        </span><span style="color: #008000;">/*</span><span style="color: #008000;"> ..and the length </span><span style="color: #008000;">*/</span>
        <span style="color: #0000ff;">if</span> (length 0<span style="color: #000000;">) {
            size </span>= num_in - offset +<span style="color: #000000;"> length;
        } </span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> (((unsigned <span style="color: #0000ff;">long</span>) offset + (unsigned <span style="color: #0000ff;">long</span>) length) ><span style="color: #000000;"> (unsigned) num_in)         {
            size </span>= num_in -<span style="color: #000000;"> offset;
        }

        </span><span style="color: #008000;">/*</span><span style="color: #008000;"> Initialize return value </span><span style="color: #008000;">*/</span><span style="color: #000000;">
        array_init_size(return_value, size </span>> <span style="color: #800080;">0</span> ? size : <span style="color: #800080;">0</span><span style="color: #000000;">);
        rem_hash </span>= &<span style="color: #000000;">Z_ARRVAL_P(return_value);
    }</span>

array_splice函数返回的是被删除的切片。这段代码的意思是,如果array_splice需要返回值,那么才创建返回数组,否则不创建,以免浪费空间。这也是一个编程小技巧,仅当需要的时候才返回。比如在函数中使用$result = array_splice(...),那么return_value_used就是true。

总结

到此本文结束,在平时编程中,应当像这两个函数实现时的做法一样,将最特殊的情况先处理掉,然后再继续,以免做了多余的判断;有需要保存新变量的时候才申请新的空间,不然会造成浪费。

 

原创文章,文笔有限,才疏学浅,文中若有不正之处,万望告知。

如果本文对你有帮助,请点下推荐吧,谢谢^_^

 

最后再安利一下,我在github有对PHP源码更详细的注解。感兴趣的可以围观一下,给个star。PHP5.4源码注解。可以通过commit记录查看已添加的注解。

更多源码文章,欢迎访问个人主页继续查看:hoohack

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn