st33l3rsfan Posted March 23, 2011 Share Posted March 23, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/231480-array-dis-array/ Share on other sites More sharing options...
trq Posted March 23, 2011 Share Posted March 23, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/231480-array-dis-array/#findComment-1191270 Share on other sites More sharing options...
st33l3rsfan Posted March 23, 2011 Author Share Posted March 23, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/231480-array-dis-array/#findComment-1191281 Share on other sites More sharing options...
st33l3rsfan Posted March 23, 2011 Author Share Posted March 23, 2011 Ok, so I turned on formatting in npp+(notepad +) and it is delimited by spaces. I changed it to tabs and it worked!! Thanks so much!! I really do appreciate the help and gentle nudge in the right direction. Cheers! Quote Link to comment https://forums.phpfreaks.com/topic/231480-array-dis-array/#findComment-1191286 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.