Home>
foreach ($filearr as $no =>$val) {
     $cols = explode (",", $val);
     $keyval = explode ("=", $cols [0]);
     if ($keyval [1] == $ID) {
         continue;
     } else {
         $newLines [] = $val;
     }
 }
 $fp = fopen ("product.txt", "a");
 fwrite ($fp, $newLines []);
 fclose ($fp);


Fatal error: Cannot use [] for reading in
I get an error. How can I write it?

php
  • Answer # 1

      

    Fatal error: Cannot use [] for reading in
      

    The second argument of fwrite () takes a String type.
    Please check the usage manual first. (php.net) fwrite

    $newLinesis an array of strings, so

    Method 1
    Write this after concatenating it with a line feed code (\ r \ n) usingimplode ().
    (php.net) implode

    Method 2
    Loop with$newLineselements inforeachorfor, and writefwrite ()for each line Please give me.

  • Answer # 2

    fwrite ($fp, implode (PHP_EOL, $newLines));

  • Answer # 3

    fwrite ($fp, $newLines [0]);Now