WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Submit Site
Forex Trading Online forex trading platform

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : columned txt file to array()?
Categories : Arrays, Strings, Regexps, PHP Update Picture
Rasmus Lerdorf
Date : Oct 12th 1998
Grade : Be the 1st to grade this Code Example
Viewed : 10014
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Rasmus Lerdorf
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

> I`m trying to read in a txt file that is formatted in this way..
> name1@someaddress.com realuser
> jmorris@someaddress.com realuser2
> ajacinto@anotheraddress.net ajacinto
>
> What i want to do is read this in an "associative" array using php.
> Basically do a:
>
> $array1 = file("filel.txt");
> $assocarray = array($array1);
> //then i can read and write to it via array functions.. ( key(),
> asort(),list().. etc..etc..)
>
> Can anyone give me some pointers on how to do this.. thanks..

You didn`t say which one you wanted to be the index and which one should
be the value. Anyway, I would do it like this:

$fp=fopen("file.txt","r");
while(!feof($fp)) {
$line=fgets($fp,256);
list($key,$value) = split("[[:space:]]+",$line);
$array[$key] = $value;
}
fclose($fp);

/* Loop through your array like this */
while(list($key,$value) = each($array)) {
echo "array[$key] = $value\n";
}

This script would give you:

array[name1@someaddress.com] = realuser
array[jmorris@someaddress.com] = realuser2
array[ajacinto@anotheraddress.net] = ajacinto


Obviously if you wanted to switch the key and the value, you could do that
easily in the script.

I didn`t use the file() function because I don`t see any reason to load
the entire file into memory and have multipe complete copies of the
information eating up your memory. This version goes line by line and
doesn`t use very much memory in case your file might be large.

And, if the separator between the address and the user id is a known
character or string, use the explode() function instead of the split()
function. I wrote it so any amount of whitespace could be between the two
strings, but if you can get away with using explode() instead you should
since it is much more efficient to avoid regular expressions.



Avoiding or Detecting high bit characters in a string. Useful when you want to create a valid RSS feed
Categories : PHP, Strings, Unicode, Regexps, Rich Site Summary (RSS)
BBCode Formatting String
Categories : PHP, HTML, Regexps, Arrays
Tag content retrieval from websites with preg_match
Categories : PHP, Regexps, Arrays, HTML and PHP
I need a trim function/regexp that will trim all " " from the ends of a string.
Categories : Regexps, PHP, Strings
PHP Script to find url links in a page
Categories : PHP, URLs, Regexps, Arrays
Check for functional file links (broken Files)
Categories : PHP, Data Validation, FTP, Regexps, Arrays
How to ifconfig down/up a list of IP's
Categories : Arrays, Strings, Filesystem, PHP
Takes an array and returns a string, suitable for inputing in an SQL statement
Categories : Arrays, Strings, PHP
Grab images from one or more URLs and save them to a specified local directory.
Categories : PHP, Filesystem, Strings, Arrays
Making sure a string containes only digits or no digits.
Categories : Strings, PHP, Regexps
String Replacement and speed consideration
Categories : PHP, Strings, Regexps
Finding numbers within a string
Categories : PHP, Regexps, Strings
Compare two texts and display a block of text with the differences between them.
Categories : PHP, PHP Classes, Filesystem, Strings, Arrays
Function to create a separated list
Categories : PHP, Arrays, Strings
Variable serialization and unserialization. Loading and saving variable structures to and from file.
Categories : Arrays, Filesystem, Variables, Strings, PHP