Results 1 to 3 of 3

Thread: Biased Percentages Based On Array

  1. #1
    Join Date
    Jul 2011
    Posts
    56
    Thanks
    5
    Thanked 1 Time in 1 Post

    Post Biased Percentages Based On Array

    Hi!

    I've been racking my brain to find a way to do this but with no luck.

    I have a string that contains items with corresponding percentages:

    $string="item1.20|item2.60|item3.20";

    I need to explode this string twice and put in an array so that I can have the array look like this:

    $items=array("item1" => "20", etc...)

    Then I will have the script generate a random number between 1 and 100 and if that number determines the item that is chosen based on its percentage value, it saves that value as another final variable.

    Does anyone have any idea how I can do this?

    Thanks a ton in advanced,

    M2com

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,373
    Thanks
    77
    Thanked 3,421 Times in 3,382 Posts
    Blog Entries
    12

    Default

    Given time, I would probably simplify this some. But here's a fairly good approach assuming I understand the question:

    PHP Code:
    <pre><!-- see below comment on the closing /pre tag (this is for visual formatting) -->
    <?php
    $string
    ="item1.20|item2.60|item3.20";
    $ar1 explode('|'$string);
    $percent 0;
    foreach(
    $ar1 as $item){
        
    $ar2[] = explode('.'$item);
        
    $ar2[count($ar2) - 1][1] = ($percent += $ar2[count($ar2) - 1][1]);
    }
    $rnum rand(1$percent);
    $randomresult $ar2[0][0];
    print_r($ar2); // just so you can see the multidimensional array, may be removed or commented out
    foreach ($ar2 as $items){
        if(
    $rnum <= $items[1]){
            
    $randomresult $items[0];
            break;
        }
    }
    echo 
    "\n$rnum"// just so you can see the random number, may be removed or commented out
    echo "\n$randomresult"// this is the result. You can leave this line as is, or comment it out/remove it and do something else later with $randomresult
    ?></pre> <!-- the pre tag is just for visual formatting, if you are doing other thins with 
    the code that either have their own format, or that aren't visual, it can be removed -->
    Last edited by jscheuer1; 11-20-2014 at 06:29 AM. Reason: minor code improvement - now allows any number as total percentage
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. The Following User Says Thank You to jscheuer1 For This Useful Post:

    M2com (11-21-2014)

  4. #3
    Join Date
    Jul 2011
    Posts
    56
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default

    Worked perfectly! Thanks a ton!

Similar Threads

  1. Calculator working out percentages
    By jelly46 in forum JavaScript
    Replies: 2
    Last Post: 08-23-2013, 04:27 PM
  2. Accessing dynamic div based off array.
    By lmbarns in forum JavaScript
    Replies: 1
    Last Post: 01-22-2012, 04:34 PM
  3. Page background color based on array
    By dave_mcafee_39 in forum Looking for such a script or service
    Replies: 2
    Last Post: 11-24-2008, 11:07 PM
  4. ??? about percentages
    By apoc.3.21 in forum HTML
    Replies: 1
    Last Post: 07-25-2008, 07:47 PM
  5. percentages and ie
    By james438 in forum CSS
    Replies: 1
    Last Post: 08-10-2007, 07:21 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •