尝试使用unset删除数组中的值。 PHP

时间:2013-12-08 10:19:54

标签: php arrays unset

$file读取元素到数组,返回值array[0]然后删除元素并将数组保存到文件 未设置无效,为什么?
这是代码:
list.txt - file(1kb)10行。

    function files($files)
    {

        $list = array();
        $list = file($file) or die('read error');

        print_r($list); //debug


        unset($list[0]);

        $f = fopen($file, 'w+');
        flock($f, LOCK_EX);
        foreach ($list as $string) {
            fwrite($f, $string);
        }
        print_r($list); // debug
        flock($f, LOCK_UN);
        fclose($f);
return $s = ($list[0]);
}

1 个答案:

答案 0 :(得分:0)

在unset-statement之前返回一个值:

function files()
{ 

    $list = array();
    $list = file($file) or die('read error');

    print_r($list); //debug

    return $s = ($list[0]);
    // Every statement after this line wouldn't be called, because you
    // already returned

    unset($list[0]);

}

尝试

$result = $list[0];
// And at the end of your function
return $result;