Jump to content

Recommended Posts

In the script below what I am trying to do is to create a counter whereby it takes a random variable from the arrays $stats adds it's value (taken from a mysql table and stored in a variable) to a string and then unsets the element so that on the next loop it will not show the value again. But when i try it, it displays more than one of the same element and i'm not sure why. I know it's a simple problem but could anyone point me in the right direction.


[code]
    $numElements = count($stats);
    $numElements += "1";
    $ss= "0";
    $stats= array("command", "power", "agility", "knowledge", "acumen", "charisma");
    
    for($i; $i < $numElements; $i++){    
      $dispstats= array_rand($stats);
      $disp= $stats[$dispstats];
      if($disp == "command"){
        $dispfriend= $dispfriend. "-Command: ".$sfcomm."-";
        unset($stats['command']);
        $ss++;  
      }
      else if($disp == "power"){
        $dispfriend= $dispfriend. "-Power: ".$sfpow."-";
        unset($stats['power']);
        $ss++;
      }
      else if($disp == "agility"){
        $dispfriend= $dispfriend. "-Agility: ".$sfagil."-";
        unset($stats['agility']);
        $ss++;
      }
      else if($disp == "knowledge"){
        $dispfriend= $dispfriend. "-Knowledge: ".$sfknow."-";
        unset($stats['knowledge']);
        $ss++;
      }
      else if($disp == "acumen"){
        $dispfriend= $dispfriend. "-Acumen: ".$sffacu."-";
        unset($stats['acumen']);
        $ss++;
      }
      else if($disp == "charisma"){
        $dispfriend= $dispfriend. "-Charisma: ".$sfchar."-";
        unset($stats['charisma']);
        $ss++;
      }
      if($ss == $statview){
        break;
      }    
    }
[/code]

Help appreciated as always
-Tjk
Link to comment
https://forums.phpfreaks.com/topic/10894-array-unset-bug/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.