|
|
|
> 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 | |
|
|
|