Question

Reading elements from $file to array, return value of array[0] then delete element and save array to file unset is not working, why ?
here is code:
list.txt - file(1kb) 10 lines.

    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]);
}
Was it helpful?

Solution

You return a value before your 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]);

}

Try

$result = $list[0];
// And at the end of your function
return $result;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top