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