Re: Help!! with array's Please
| From: | Tom Rogers | Date: | Fri, 28 Feb 2003 03:39:52 +0000 |
| Subject: | Re: Help!! with array's Please | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-137504@lists.php.net to get a copy of this message | ||
Hi,
Friday, February 28, 2003, 12:20:38 PM, you wrote:
RK> I really need somebody to help me with this I am totally lost on what
RK> to do
RK> I need a way to read the following text file and add to or delete from
RK> or change the data.
RK> I have been able to move it all into an array using this code
RK> $groups= file("group");
RK> for ($i=0; $i<$number_in_group; $i++){
RK> $groups[0];
RK> $groups[3]; <-- this is the part of the array that I need to change
RK> }
RK> But I can not figure out how to search the array and delete from or add
RK> to the array at a given point. I would like to say add another user to
RK> the end of site6 or delete a user from site3.
RK> Could somebody give me a hand here. I have read the manual for arrays
RK> and still can't figure it out.
RK> site1:x:503:tester1
RK> site2:x:504:tester2,tester2a
RK> site3:x:505:tester3,tester3a,tester3b
RK> site4:x:506:tester4
RK> site5:x:507:tester5,tester5a,tester5b
RK> site6:x:508:tester6
RK> site7:x:509:tester7,tester7a,tester7b
RK> --
RK> Best regards,
RK> Richard mailto:rdkurth@starband.net
your groups array will look like this
groups[0] = site1:x:503:tester1
groups[1] = site2:x:504:tester2,tester2a
so loop through the array
$x = 0;
while(list($key,$val) = each($groups)){
//you need split the array values like this
list($name,$pass,$gid,$user_list) = split (":", $groups[$x]);
//see if we have the right one
if($name = $wanted_name){ //site6
//then split the usernames into a sub array
$users = explode(',',$userlist);
// to add
$users[] = $newuser;
// to delete
unset($users[3])
// now add it back
$userlist = implode(',',$users)
$list = $name.':'.$pass.':'.$gid.':'.$userlist;
$groups[$x] = $list;
}
$x++;
//here you could fputs to a temp file then copy to the original
// after finished
}
Then write groups back to disk
--
regards,
Tom