Note that there are some explanatory texts on larger screens.

plurals
  1. POtwo-dimension array after fgets?
    text
    copied!<p>I´m learning to work with files in php, first i made this function for writing to the file, it´s quite simple:</p> <pre><code>function zapis_do_suboru($zapisovany_subor, $obsah_suboru) { $pracovny_subor = fopen($zapisovany_subor,"w") or die("Chyba pri otvarani suboru"); fwrite($pracovny_subor, $obsah_suboru) or die("Nejde zapisovat do suboru"); fclose($pracovny_subor); echo "Zapis $zapisovany_subor prebehol uspesne."; } </code></pre> <p>Then I made function for reading from the file, but I was little stuck here, because my book is explaining how to read just one row, but then i googled little bit and found some solution and made this function for reading from file:</p> <pre><code>function citanie_zo_suboru($citany_subor) { $pracovny_subor = fopen($citany_subor,"r") or die("Chyba pri otvarani suboru"); $j = 0; while(!feof($pracovny_subor)) { $pole[$j] = array(fgets($pracovny_subor, 4096)); $j++; } fclose($pracovny_subor); return $pole; } </code></pre> <p>Then i wanted to test it, so i create two variables:</p> <pre><code>$subor = "textsubor.txt"; $text = &lt;&lt;&lt;_END Riadok1 blabla Riadok2 blabla Riadok3 meno suboru: $subor _END; </code></pre> <p>And this calling of functions:</p> <pre><code>zapis_do_suboru($subor, $text); echo "&lt;br /&gt;"; foreach (citanie_zo_suboru($subor) as $index =&gt;$popis) echo $popis."&lt;br /&gt;"; </code></pre> <p>But the problem is, that function citanie_zo_suboru is creating two-dimension array, so the output was only "array array array array". With print_r and little bit of trying i found out, that when i change:</p> <pre><code>foreach (citanie_zo_suboru($subor) as $index =&gt;$popis) echo $popis."&lt;br /&gt;"; </code></pre> <p>to:</p> <pre><code>foreach (citanie_zo_suboru($subor) as $index =&gt;$popis) echo $popis[0]."&lt;br /&gt;"; </code></pre> <p>it's doing exactly what i wanted. Can someone tell me why my function is creating two-dimension and not just classic one-dimension array? I would be really grateful if you could. Thanks</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload