Jump to content

array dis-array


st33l3rsfan

Recommended Posts

All,

So I have a text file I'm reading into an array that looks like this:

TEST.TXT

00501      NYHoltsville                    Suffolk                        840

00544      NYHoltsville                    Suffolk                        840

00601      PRAdjuntas                    Adjuntas                    840

00602      PRAguada                      Aguada                      840

00603      PRAguadilla                    Aguadilla                  840

 

I read this into an array using this snippet:

 

if ($zips=file("test.txt")){

$i=0;

foreach($zips as $zip){

echo "\$zips[$i] => $zip.\n";

$i++;

}

}

My output is this:

zips[0] => 00501 NYHoltsville Suffolk 840 . $zips[1] => 00544 NYHoltsville Suffolk 840 . $zips[2] => 00601 PRAdjuntas Adjuntas 840 . $zips[3] => 00602 PRAguada Aguada 840 . $zips[4] => 00603 PRAguadilla Aguadilla 840 .

 

What I want to do is break the index up so say, zips[0] would be $zipc =00501, $state=NY, $city=Holtsville, $county=Suffolk, $countryCode=840.

 

I have read chapters on arrays and consulted php.net but I feel the answer is eluding me and perhaps I don't know what to look for exactly.

Any help is appreciated!  Thanks so much! in advance.  You can email me at  playa86 AT gmail DoT com.

Link to comment
https://forums.phpfreaks.com/topic/231480-array-dis-array/
Share on other sites

You need to explode into a new array using a delimiter (looks like tab from here), the problem you have is that the state and city don't follow the same pattern (eg; they are joined). Is this what your actual data looks like?

 

Something like....

 

if ($lines = file("test.txt")) {
   foreach ($lines as $line) {
      list($zip, $city, $county, $code) = explode("\t", $line);
      echo "zip = $zip, city = $city, county =  $county, code = $code";
   }
}

 

If you state and city really are joined you'll need to use substr to break them apart.

Link to comment
https://forums.phpfreaks.com/topic/231480-array-dis-array/#findComment-1191270
Share on other sites

Thanks so much for the direction!  I follow the logic for sure.  When I tried to run it I got this error

Notice: Undefined offset: 3 in C:\wamp\www\transit\write_file.php on line 34

Notice: Undefined offset: 2 in C:\wamp\www\transit\write_file.php on line 34

Notice: Undefined offset: 1 in C:\wamp\www\transit\write_file.php on line 34

zip = 00501 NYHoltsville Suffolk 840 , city = , county = , code =

 

It still wants to shove everything in zip.  I tried using the " " space delimiter but it just assigned an index to every character.  Any thoughts?  Again thanks.

Link to comment
https://forums.phpfreaks.com/topic/231480-array-dis-array/#findComment-1191281
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.