user1905494
user1905494

Reputation: 55

echoing foreach loop

i have the following code

 $contents = file_get_contents('folder/itemtitle.txt');
$fnamedata = file_get_contents('folder/fname.txt');
$fnamearray = explode("\n", $fnamedata);
    $contents = explode("\n", $contents);
    foreach ($contents as $key => $itemline)


    {
    }


    foreach ($fnamearray as $key2 => $fname)
    {

    echo ($fname);
    echo ($itemline);
    }

what i want to do is to have the first line of each file echo so the output looks like

fname[0},itemline[0],fname[1],itemline[1]

what i am getting with the following is just this fname[0],fname[1],fname[2].... ect

h

Upvotes: 2

Views: 58

Answers (2)

BenM
BenM

Reputation: 53198

Assuming the indexes will always match:

$contents = file_get_contents('folder/itemtitle.txt');
$fnamedata = file_get_contents('/home/b1396hos/public_html/ofwgkta.co.uk/dd_folder/fname.txt');
$fnamearray = explode("\n", $fnamedata);
$contents = explode("\n", $contents);

for($i = 0; $i < count($contents); $i++)
{
    echo $fnamearray[$i];
    echo $contents[$i];
}

Upvotes: 2

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324640

Since both arrays are simple, consecutive numeric indexed arrays, you can just use a for loop:

$l = max(count($fnamedata),count($contents));
for($i=0; $i<$l; $i++) {
  $itemline = $contents[$i];
  $fname = $fnamearray[$i];
  // do stuff
}

Upvotes: 2

Related Questions