Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6589427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:10:25+00:00 2026-05-25T17:10:25+00:00

Hy everyone, I’m having trouble with properly nesting while loops to read from 2

  • 0

Hy everyone, I’m having trouble with properly nesting while loops to read from 2 arrays.
I have 2 files from which I read the content:

file1: item_list.txt
string1 \n
string2 \n
string3 \n
...

file2: item+info.txt
string3 \t info1 \t info2 \t info3
string1 \t info7 \t info1 \t info4
string5 \t info2 \t info3
string2 \t info2 \t info4 \t info1

(values are separated by new lines and tabs only, I added one space between characters here just to increase readability).

I read from files using fgetcsv() function, and each row from file is stored as an array into a variable $data. I created a while loop with condition (!feof($fp)) to read through the file until the last row. But I can’t quite properly nest the second loop.

What I want to do with this:
read the first string found in file1, go to file2 and try to find that string. If there’s a match, get the info data for that string (all of the data, or just one, doesn’t matter). If there’s no match, return message “no match”. In either case, once the second loop has done it’s thing, I need to read the second string in file1, and do the search in file2 again. Repeat this as long as there is something to read from the file1.

here are two versions of my code, they don’t work, and I can’t figure out why.

//opening the files
$fp = fopen("$DOCUMENT_ROOT/test/item_list.txt", "r"); #raw item list
$pf = fopen("$DOCUMENT_ROOT/test/item+info.txt", "r"); #item+info list

//read from first file
$line=0;
while (!feof($fp)){
    $line++;
    $data1 = fgetcsv($fp, "1000", "\n");
    $item1= $data1[0];
    echo "line: $line. item_list: ".$item1; //just to see on screen what's happening
    print_r($data1); //same here, just to see what's going on
    echo"<br />";

    //searching for string in file2
    $row=0;
    while (!feof($pf)){
        $row++;
        $data2 = fgetcsv($pf, "1000", "\t");
        $item2= $data2[0];
        echo "line: $row. item+info: ".$item2; //just checking things on screen
        print_r($data2); //here too
        echo "<br />";

        //conditioning
        //equal strings
        if ($string1== $string2)
            echo $data2[1]."<br />";
        break;
    }
}

fclose($fp);
fclose($pf);

this used to work as long as the items in item_list.txt and item+info.txt are oredered
exactly the same (string1\nstring2\string3 ->
string1\tinfo1\nstring2\tinfo2\nstring3\tinfo3 – but that’s never going to happen in my
case, it’s impossible to order the items like that)

I tried to do it with foreach() statement do itterate through arrays, but the result is something that I can’t make any sense out of.

while (!feof($fp)){
    $data1 = fgetcsv($fp);
    foreach ($data1 as $token1) {
        while (!feof($pf)) {
            $data2 = fgetcsv($pf);
            foreach ($data2 as $value) {
                explode ("\t", $value);
                if ($token1 == $value[0])
                    echo $value[1];
            } 
            break;
        } 
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T17:10:27+00:00Added an answer on May 25, 2026 at 5:10 pm

    This should do it:

    $file1 = file($DOCUMENT_ROOT . '/test/item_list.txt');
    $file2 = file($DOCUMENT_ROOT . '/test/item+info.txt');
    
    foreach ($file1 as $line)
    {
        $line = rtrim($line); // just in case ...
        if ($line === '') continue;
    
        foreach($file2 as $infoline)
        {
            $infoline = explode("\t", rtrim($infoline);
            if ($line === $infoline[0])
            {
                array_shift($infoline);
                echo $line . '<br /><br />' . implode('<br />', $infoline);
                // $results[$line] = $infoline; // uncomment this if you need the search results stored for later use
                break;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

everyone,i have a lot of files write to disk per seconds,i want to disable
everyone The problem is if there exists two exported components which have the same
Everyone in my office uses Macs and therefore most use Safari. We have a
everyone, I've started working yesterday on the Euler Project in Clojure and I have
everyone. I used Ruby and Rails before, and I enjoy Rails's ORM, which make
everyone! i have a question about sessions hopefully someone can help me with. I
everyone. I have an advanced T4 question and I'm hoping someone can help. I've
Everyone, I am a newbie to android development. Now I have a question that
everyone, I have this piece of the code: void foo(int var, int var1) {
everyone! I've been at this for a while, and I'm not sure how this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.