Re: Question about a text file and foreach
| From: | David T-G | Date: | Tue, 18 Mar 2003 16:49:02 +0000 |
| Subject: | Re: Question about a text file and foreach | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-140139@lists.php.net to get a copy of this message | ||
Jim --
Please don't hijack threads. This message has nothing to do with calling
a function in a page via GET. When you wish to ask a fewsh and unrelated
question, please send a fresh and unrelated message. [I keep hearing
good things about Evolution. Doesn't it have decent threading?]
..and then Jim Greene said...
%
% Hi All,
% I have a text file that has entries like the following:
% user1:mbox1:
% user1:mbox2:
Good enough.
%
% I basically do: $mboxs = `cat file | grep user1 | cut -d: -f2';
Why?
% I then want to print out the data in the $mboxs variable (array)
% I am trying to use foreach but it does not work.. I am assuming the data is not considered an
array?
Right; all you've done is create a string variable with a bunch of
entries in it.
% Any help would be appreciated.. Thanks
I'd just fopen it, myself, rather than messing with the system call:
$fh = fopen("/tmp/file",'r') ;
while ( ! feof($fh) and list($user,$mbox) = explode(":",trim(fgets($fh))) )
{ $mboxes[] = $mbox ; }
fclose($fh) ;
print_r($mboxes) ;
If you want to maintain the relationship of userN to mboxN, you could
instead say
{ $mboxes[$user] = $mbox ; }
and then later address them by name instead of number.
% Jim
HTH & HAND
:-D
--
David T-G * There is too much animal courage in
(play) davidtg@justpickone.org * society and not sufficient moral courage.
(work) davidtgwork@justpickone.org -- Mary Baker Eddy, "Science and Health"
http://justpickone.org/davidtg/ Shpx gur
Pbzzhavpngvbaf Qrprapl Npg!