0
<?php

$userName = array();
$tutorial = array();
$myFile = "students.txt";
$fh = fopen($myFile,'r');
while( !feof($myFile) ){
    $userName[] = array(fgets($fh));//Save first line content
    $tutorial[] = array(fgets($fh));//Save second line content
}
fclose($myFile);
echo "$userName";
echo "$tutorial";
?>

and my students.txt content

dasdsa
A
asdasd
D

How to read that and store into different array and print them out

Felicia Tan
  • 1,547
  • 2
  • 12
  • 9
  • What output you getting using above method ? Also `$username` is array, us `print_r($userName);` to check output & not echo. – Rikesh Jun 13 '13 at 10:25

4 Answers4

1

your code should work as expected. I assume you're bit confused with echo "$userName"; output as it displays Array word. try var_dump($userName) instead

heximal
  • 10,327
  • 5
  • 46
  • 69
0

Use function file() in PHP

file — Reads entire file into an array

$array_lines = file('students.txt');
$count =  count($array_lines);

$first_arr = array();
$sec_arr = array();
foreach ($array_lines as $i => $line){
   if($i%2) $first_arr[] = $line;
   else $sec_arr[] = $line;
}

print_r($first_arr);
print_r($sec_arr);

With file() function every line is read as element in array. You can check it with:

print_r($first_arr);
Robert
  • 19,800
  • 5
  • 55
  • 85
  • and loop over $array_lines with foreach($array_lines as $line_number=>$line) – HarryFink Jun 13 '13 at 10:27
  • Surely the loop would start at 1 wouldn't it? – h2ooooooo Jun 13 '13 at 10:27
  • @Robert Because you used `++$i` instead of `$i++` meaning it should add 1 to `$i` FIRST instead of after the iteration. **Edit**: It seems I am wrong - it makes no difference what so ever. – h2ooooooo Jun 13 '13 at 10:32
  • @h2ooooooo you gotta be joking me :) ++$i is done after loop is finished preincrementation is faster than postincrementation that is why I don't use preincremenation here. Do a simple for loop and try it for yourself. If you don't believe I can create you a fiddle. I don't do `echo $array_lines[++$i]` :) – Robert Jun 13 '13 at 10:36
  • 1
    @Robert [this](http://stackoverflow.com/questions/6400518/pre-incrementation-vs-post-incrementation) surely made it much clearer - I guess I learned something new today (even though, pre-optimization - root of all evil - yatayata). – h2ooooooo Jun 13 '13 at 10:39
  • i want store into array and not print it . can u guys please see what is my question.... – Felicia Tan Jun 13 '13 at 10:41
  • @h2ooooooo I created fiddle for you http://phpfiddle.org/main/code/c5s-66k run this code. – Robert Jun 13 '13 at 10:42
  • "*i want store into array and not print it*" "*How to read that and store into different array and **print them out***". Huh? – h2ooooooo Jun 13 '13 at 10:47
  • @robert. yours 1 only working. but then can u store into more easier way ? just like when i echo the first index of username and first index of tutorial – Felicia Tan Jun 13 '13 at 10:54
  • so simply put the following code `$array_lines = file('students.txt'); $firstVar = $array_lines[0]; $secondVar = $array_lines[1]; echo $firstVar.$secondVar;` – Robert Jun 13 '13 at 11:20
0

Do exactly as you've done, but change

$userName[] = array(fgets($fh));//Save first line content
$tutorial[] = array(fgets($fh));//Save second line content

to

$userName[] = fgets($fh);//Save first line content
$tutorial[] = fgets($fh);//Save second line content

(no need to keep the subitems in their own seperate array)

and print them out by either using print_r, or iterate through them:

for ($i = 0; $i < count($userName); $i++) {
    echo $userName[$i] . " - " . $tutorial[$i];
}
h2ooooooo
  • 39,111
  • 8
  • 68
  • 102
  • @FeliciaTan It sounds like you have a problem somewhere else. What does `print_r($userName)` and `print_r($tutorial)` reveal? Could you post said output in the OP? – h2ooooooo Jun 13 '13 at 10:40
0
$text = file_get_contents('students.txt');
$text = explode("\n",$text);
$output = array();
foreach($text as $line)
{  
    $output[] = $line;
}
Irfan DANISH
  • 8,349
  • 12
  • 42
  • 67