title: Random value from array by weight in php
body: <p>I understand that this question has likely been asked, but I don't understand the prior questions enough to know if they do what I want.</p>
  
  <pre><code>$fruits = array('20' =&gt; 'apple', '40' =&gt; 'orange', '40' =&gt; 'pear');
  </code></pre>
  
  <p>The keys are percentages of the chance of value getting picked. I would like to mt_rand() a number between 0 and 99 and return a value from $fruits based on those percentages.</p>
  
  <p>It's very possible I'm so confused because I don't know how to explain what I'm looking for.</p>
  
  <p>Thank you in advance for the help. </p>
  
  <p>Edit: want a random value from $fruits, based on these chances:</p>
  
  <p>I want a 40% chance of getting an orange, a 40% chance of getting a pear, and an 80% chance of getting an apple.</p>
  
  <p>Edit: To further clarify, since either a lot of the answers got it wrong, (or I just don't understand their code), I needed a result regardless of what number I pick, not just 20, 40, or 40.</p>