Page 1 of 1
Delete from array
Posted: Wed Jul 16, 2003 10:53 pm
by DuFF
Ok, I'm making a function that creates 8 teams of 3 users. The 24 usernames are stored in a text file and are seperated by a "|". I explode all the values and store them in an array. Then I use array_rand() to pick a random user from the array and then I can put him on a team. The problem is that I don't want someone to be picked twice. I've been trying to solve this for about an hour but have come up with nothing. Theres no function to delete a value from an array unless its the first or last value, but of course you can't move 'em either. If I'm wrong plz correct me.
Heres what I have:
Code: Select all
<?php
$thefile=file($usersfile);
$namesarray=explode("|", $thefile[0]);
array_shift($namesarray);
$memname = array_rand ($namesarray);
?>
How can I make sure noone is picked twice? Any help is greatly appreciated.
Posted: Wed Jul 16, 2003 11:32 pm
by Gen-ik
You could always use another array to store the already-chosen names in.
For example... this script presumes that apple has already been chosen. But obviously you can add names to the $used_list array.
Code: Select all
<?php
$name_list = Array('apple','banana','cherry');
$used_list = Array('apple');
//lets say your code has chosen banana this time
$new_name = 'banana';
//now loop through the $used_list and see if banana is in there
$is_in_there = false;
for($i=0; $i<count($used_list); $i++)
{
if($used_list[$i]==$new_name) { $is_in_there = true; }
}
//now do something depending on the results
if($is_in_there)
{
//the new_name IS in the used_list so do something..
}
else
{
push_array($used_list, $new_name);
//the new_name has been added to the used_list
//now do something else...
}
?>
I haven't checked it for errors but the idea behind the code should work

Posted: Thu Jul 17, 2003 3:26 am
by Wayne
will delete 2nd value in the array, replace 1 with whatever value you want to remove from the array.
Posted: Thu Jul 17, 2003 6:20 am
by patrikG
Store the key of the array-element you are displaying.
Then use either
unset() (see Waynes post above)
or
array_splice()
the difference is that unset() won't re-index the array, while array_splice() does.
Posted: Thu Jul 17, 2003 8:54 am
by DuFF
Yeah, I looked at that function but it says: Remove a portion of the array and replace it with something else. The problem is that I don't want to replace it with something, I just want it to delete the portion. Plus, even after reading it more carefully I don't get the offset/length part of array_splice. And the examples just confuse me even more

.
Posted: Thu Jul 17, 2003 2:29 pm
by xisle
along Gen-ik's line of thought...
Code: Select all
$ckname = array_rand($namesarray);
if(!in_array("$ckname", $used)){
$memname=$ckname;
$usedї]=$ckname;
}
Posted: Thu Jul 17, 2003 6:43 pm
by DuFF
After a little editing it worked! Thanks a lot xisle and everyone else that helped.
Only one little problem, array_rand returns the the [VAR] number, not the actual value held in the array. So I just edited xisle's code to this:
Code: Select all
<?php
$cname = array_rand($namesarray);
$ckname = $namesarray[$cname];
if(!in_array("$ckname", $used)){
$memname=$ckname;
$used[]=$ckname;
}
?>
Posted: Thu Jul 17, 2003 8:41 pm
by DuFF
Acutally its not working correctly

. Heres an output from the first test:
Team 1|DuFF19|DuFF16|DuFF!|
Team 2|DuFF21|DuFF6|DuFF!|
Team 3|DuFF18|DuFF3|DuFF14|
Team 4|DuFF18||DuFF12|
Team 5|DuFF17|DuFF9|DuFF8|
Team 6|DuFF17|DuFF10|DuFF8|
Team 7|DuFF11|DuFF4|DuFF8|
Team 8|DuFF2|DuFF4|DuFF8|
I realized (look at the code in the above post) that the $used[] array wasn't working and also that I have no else statement to go with the if statement. So if a name does equal a name in the $used[] array, it won't print anything. I'm guessing thats what happened on Team 4. The thing is how can I have my else statement redo the rand_array and then check it to the $used[] array again? It would just go on forever because it would have to keep checking it over and over in differnt if statements.
Can anyone help me? Thanks
Posted: Fri Jul 18, 2003 10:39 am
by xisle
by giving snippets we force you to work at it a bit harder.
I used a couple of loops for a solution close to what you are after.
Code: Select all
<?php
// build test name array, use your file for this step
for($i=0; $i<24; $i++){
$namesarray[]="DUFF".$i;
}
// end test array
$total_available_players = count($namesarray);
$total_players_per_team = 3;
$total_teams_to_populate = 8;
$t=1;
$p=0;
while($t <= $total_teams_to_populate){
print"<p>team $t contains:";
while($p < $total_players_per_team){
$randomkey = array_rand($namesarray);
$ckname = $namesarray[$randomkey];
if(!in_array("$ckname", $used)){
$memname=$ckname;
$used[]=$ckname;
print" | $memname";
$p++;
}
}
unset($p);
$t++;
}
?>