1

I am trying to import 2 separate text files into an array then echo the result. One file contains street names, the other contains street descriptions:

for example:

Street Name - Its a nice street.

So far I am able to import the 1st file into an array and see the result using a foreach loop.

When I try to nest another foreach loop within the first one i get nothing. Trying to figure out what i am doing wrong.

My apologies, i am really new to PHP.

<?php    
$lines = file('files/streetnames.txt');
$lines2 = file('files/streetDescriptions.txt');

foreach ($lines as $streetName => $line) {
    echo htmlspecialchars($line);

foreach($lines as $streetDes => $line2){
echo htmlspecialchars($line2);
break;

}

}
?>

The data in the 1 text file(streetNames.txt):

Anderson Mill Road

The data in the second file is (streetDescriptions.txt) is

Named after a mill and its owner, Thomas Anderson, who lived in the vicinity in the 1850s.

The result I am trying to achieve is:

Anderson Mill Road - Named after a mill and its owner, Thomas Anderson, who lived in the vicinity in the 1850s.

Wil
  • 49
  • 1
  • 7
  • you have a typo in this line : `foreach($lines as $streetDes => $ $line2)` too many `$` after `=>`. Also, your `$streetDes` are not in `$lines` but `$lines2` according to your filenames. – Shawn Mehan Oct 24 '15 at 04:45
  • Thanks Shawn, i fixed those typos, now i get 1 big long output with everything repeating over and over again, I added a break; after the last line, but I still can't get it to output correctly – Wil Oct 24 '15 at 04:54
  • so, do you want to update your question with what you have now. No point in everyone else having to debug typos. – Shawn Mehan Oct 24 '15 at 04:56
  • Update your qus, with sample data of streetnames and streetDesc files and what is your expecting output, – Jitendra Kumar. Balla Oct 24 '15 at 04:57
  • Sorry about that, Noob mistake. I updated the question. Thank you all for all you help, it's very much appreciated. – Wil Oct 24 '15 at 05:11

0 Answers0