I need some help and I gladly appreciate any i can get

I am reading a file and putting the data into an array

$data = file('test.txt');
foreach ($data as $line){
    $fields = explode("\t", $line);
    $id = $fields[0];
    $name = $fields[2];
    $street = $fields[3];
    $city = $fields[7];
    $state= $fields[8];
    $zip = $fields[9];
    $country = $fields[10];
    if ($country == 'United States') {
        $ushotels = $line;
                 foreach ($ushotels as $usdata){
                            $fields = explode("\t", $usdata);
                            $uscountry = $usdata[10];
                            echo $uscountry;
                 }


} else {
    $foreignhotels = $line;
}

}









?>

the 10th level or field of the file is a country, what I am trying to do is that if the county equals the 'United States', i want to write the contents of the orginal $data file to one array
and
if the country does not equal 'United States' place the $data into another array

I know my code looks like $@#@, but I am new to this

can someone help me out please..................

Regards
Mike

    "$ushotels[] = $line" and "$foreignhotels[] = $line" will put the lines into their respective arrays.

    You need to place the "foreach ($ushotels..." loop outside the other foreach. The way you have it now, it will execute each time you add a new line to the variable, before it's filled.

    Edit: Although now that I read it closer I'm not sure what the second foreach is for.

      Installer wrote:

      "$ushotels[] = $line" and "$foreignhotels[] = $line" will put the lines into their respective arrays.

      You need to place the "foreach ($ushotels..." loop outside the other foreach. The way you have it now, it will execute each time you add a new line to the variable, before it's filled.

      installer

      thanks for the help but I dont understand what you mean, can you give me an example of putting the foreach $ushotels outside

      sorry i am just a little confused

        the second foreach is to access the data in the two different arrays, like i did for the first

        does that make sense?

          If all you want is the two arrays (us and foreign), then you don't need the second ("foreach ($hotels as $usdata)") loop. If you want to display the us data lines, or do something else with them, then put the loop at the bottom, after the first loop finishes. Run this code to see what I mean:

          $data = file('test.txt'); 
          foreach ($data as $line) { 
              $fields  = explode("\t", $line); 
              $id      = $fields[0]; 
              $name    = $fields[2]; 
              $street  = $fields[3]; 
              $city    = $fields[7]; 
              $state   = $fields[8]; 
              $zip     = $fields[9]; 
              $country = $fields[10]; 
              if ($country == 'United States') { 
                  $ushotels[] = $line; 
              } else { 
                  $foreignhotels[] = $line; 
              } 
          } 
          foreach ($ushotels as $usdata) { 
              $fields = explode("\t", $usdata); 
              foreach ($fields as $key => $field)
                  echo $key . ' => ' . $field . '<br />';
              }
              echo '------------<br/>'
          }

          Edit: Yes, that makes sense. You can do that like I show here.

            i tried your script

            $data = file('test.txt');
            foreach ($data as $line) {
                $fields  = explode("\t", $line);
                $id      = $fields[0];
                $name    = $fields[2];
                $street  = $fields[3];
                $city    = $fields[7];
                $state   = $fields[8];
                $zip     = $fields[9];
                $country = $fields[10];
                if ($country == 'United States') {
                    $ushotels[] = $line;
                } else {
                    $foreignhotels[] = $line;
                }
            }
            foreach ($ushotels as $usdata) {
                $fields = explode("\t", $usdata);
                foreach ($fields as $key => $field)
                    echo $key . ' => ' . $field . '<br />';
                }
                echo '------------<br/>';
            }

            but i get this error:

            Parse error: syntax error, unexpected '}' in C:\Program Files\xampp\htdocs\worldreslistings.php on line 32

            edit: i fixed that small error man

            foreach ($ushotels as $usdata) {
            $fields = explode("\t", $usdata);
            foreach ($fields as $key => $field){
            echo $key . ' => ' . $field . '<br />';
            }
            echo '------------<br/>';
            }

            you forgot to place a '{' after the the foreach()

              Yes, there might be parse errors. I haven't built a "test.txt" file to test my code with, so I was hoping you could correct the typos yourself. The error is caused by a missing curly brace after the "foreach ($fields as $key => $field)" line.

              But here's some code that's a little less duplicative and should serve for what you want to do. The lines are put into separate us and foreign arrays, then you can work with those arrays to get your results.

              $data = file('test.txt'); 
              foreach ($data as $line) { 
                  $line = trim($line);
                  if (strchr($line, "\t") == 'United States') {
                      $ushotels[] = $line; 
                  } else { 
                      $foreignhotels[] = $line; 
                  } 
              }
              foreach ($ushotels as $hotel) {
                  list($id, $name, $street, $city, $zip, $country) = explode("\t", $line);
                  echo 'Hotel ID: ' . $id . '<br />';
                  echo 'Hotel name: ' . $name . '<br />';
                  // etc.
                  echo '----------------<br />';
              }

                thanks so much man for the help, much appreciated

                  Here's code that actually works, tested on a "test.txt" file:

                  $data = file('test.txt'); 
                  foreach ($data as $line) { 
                      $line = rtrim($line);
                      if (ltrim(strrchr($line, "\t")) == 'United States') {
                          $ushotels[] = $line; 
                      } else { 
                          $foreignhotels[] = $line; 
                      } 
                  }
                  foreach ($ushotels as $hotel) {
                      list($id, , $name, $street, , , , $city, $state, $zip, $country) = explode("\t", $hotel);
                      echo 'Hotel ID: ' . $id . '<br />';
                      echo 'Hotel name: ' . $name . '<br />';
                      // etc.
                      echo '----------------<br />';
                  }

                    I appreciate all the help yesterday, it really helped!

                    I am having some issues on how to take the data that is being split by this correction that was made. I am trying to write this to two separate files, which are us.txt and world.txt and make them identical to the input file, test.txt.

                    I am getting some strange output on the two output files and cannot figure it out. I have this script, split.php:

                    <?php
                    
                    $data = file('test.txt');
                    
                    // now loop through the array
                    foreach ($data as $line){
                        $fields = explode("\t", $line);
                    
                    // if the array has a US country marker, append the fields to the us.txt file, otherwise put them in the world.txt file
                    if ($fields[10] == "United States") {
                    	$us_file = fopen("us.txt", "a");
                    	foreach($fields as $cycle) {
                    		fwrite($us_file, $cycle."\t");
                    	}
                        //fwrite($us_file, "\n");
                        fclose($us_file);
                    } else {
                    	$world_file = fopen("world.txt", "a");
                    	foreach($fields as $cycle) {
                    		fwrite($world_file, $cycle."\t");
                    	}
                        //fwrite($world_file, "\n");
                        fclose($world_file);
                    }
                    
                    
                    }
                    
                    
                    
                    ?>

                    And I have this script, which was corrected by installer yesterday. How can I incorporate the split.php into the worldreslistings.php. Again I am new to this and am in need of some help. Here is the worldreslistings.php script and the three files.

                    <?php
                    
                    $data = file('test.txt');
                    foreach ($data as $line) {
                        $fields  = explode("\t", $line);
                        $country = $fields[10];
                    
                    if ($country == 'United States') {
                        $ushotels[] = $line;
                    } else {
                        $foreignhotels[] = $line;
                    }
                    }
                    foreach ($ushotels as $usdata) {
                        $fields = explode("\t", $usdata);
                        foreach ($fields as $key => $field){
                            echo $key . ' => ' . $field . '<br />';
                        }
                        echo '------------<br/>';
                    }
                    
                    
                    
                    ?>

                    The input and output files are attached!
                    Can someone help me out, I would truly appreciate it?

                    Regards,
                    Mike

                      $data_arr = file('test.txt'); 
                      $us_fp = fopen('us.txt', 'w');
                      $world_fp = fopen('world.txt', 'w');
                      foreach ($data_arr as $line){ 
                          $fields = explode("\t", $line); 
                          if (trim($fields[10]) == "United States") { 
                              fwrite($us_fp, $line); 
                          } else { 
                              fwrite($world_fp, $line); 
                          } 
                      } 
                      fclose($us_fp); 
                      fclose($world_fp);
                        Write a Reply...