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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.