Delete from array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Delete from array

Post 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.
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post 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 :)
User avatar
Wayne
Forum Contributor
Posts: 339
Joined: Wed Jun 05, 2002 10:59 am

Post by Wayne »

Code: Select all

unset($namesarray&#1111;1])
will delete 2nd value in the array, replace 1 with whatever value you want to remove from the array.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post 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.
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post 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 :? .
User avatar
xisle
Forum Contributor
Posts: 249
Joined: Wed Jun 25, 2003 1:53 pm

Post by xisle »

along Gen-ik's line of thought...

Code: Select all

$ckname = array_rand($namesarray);
if(!in_array("$ckname", $used))&#123;
   $memname=$ckname;
   $used&#1111;]=$ckname;
&#125;
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post 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;
}
?>
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post 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
User avatar
xisle
Forum Contributor
Posts: 249
Joined: Wed Jun 25, 2003 1:53 pm

Post by xisle »

by giving snippets we force you to work at it a bit harder.
:lol:
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++;
}
?>
Post Reply