{"id":10,"date":"2023-05-27T13:51:28","date_gmt":"2023-05-27T05:51:28","guid":{"rendered":"https:\/\/www.72bian.top\/?p=10"},"modified":"2023-05-27T13:52:10","modified_gmt":"2023-05-27T05:52:10","slug":"php%e6%8a%bd%e5%a5%96%e6%a6%82%e7%8e%87%e6%83%85%e6%99%af%e4%b8%80","status":"publish","type":"post","link":"https:\/\/www.72bian.top\/index.php\/2023\/05\/27\/php%e6%8a%bd%e5%a5%96%e6%a6%82%e7%8e%87%e6%83%85%e6%99%af%e4%b8%80\/","title":{"rendered":"php\u62bd\u5956\u6982\u7387\u60c5\u666f\u4e00"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>&lt;?php\n\n\/*\n\u5047\u8bbe\uff1a\u6709\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\uff0c\u8bb0\u5f55\u4e86\u6240\u6709\u672c\u6b21\u62bd\u5956\u7684\u5956\u9879\u4fe1\u606f\uff1a\n$test_arr =array('a'=&gt;20,'b'=&gt;30,'c'=&gt;50);\na\u5956\u6982\u738720%\uff0cb\u5956\u6982\u738730%\uff0cc\u5956\u6982\u738750%\n\u6a21\u62df\u51fd\u6570\u6267\u884c\u8fc7\u7a0b\uff1a\n\u603b\u6982\u7387\u7cbe\u5ea6\u4e3a20+30+50=100\n\u7b2c\u4e00\u6b21\u6570\u7ec4\u5faa\u73af\uff0c$procur=20\n\u5047\u8bbe\u62bd\u53d6\u7684\u968f\u673a\u6570rand(1,100)\uff0c\u5047\u8bbe\u62bd\u5230$randNum=55\n\u5982\u679c$randNum&lt;=20,\u5219result=a\n\u5426\u5219\u8fdb\u5165\u4e0b\u4e00\u5faa\u73af\uff0c\u603b\u6982\u7387\u7cbe\u5ea6\u53d8\u4e3a100-20=80\n\u7b2c\u4e8c\u6b21\u6570\u7ec4\u5faa\u73af\uff0c$procur=30\n\u5047\u8bbe\u62bd\u53d6\u7684\u968f\u673a\u6570rand(1,80)\uff0c\u5047\u8bbe\u62bd\u5230$randNum=33\n\u5982\u679c$randNum&lt;=30,\u5219result=b\n\u5426\u5219\u8fdb\u5165\u4e0b\u4e00\u5faa\u73af\uff0c\u603b\u6982\u7387\u7cbe\u5ea6\u53d8\u4e3a80-30=50\n\u7b2c\u4e09\u6b21\u6570\u7ec4\u5faa\u73af\uff0c$prosur=50;\n\u5047\u8bbe\u62bd\u53d6\u7684\u968f\u673a\u6570rand(1,50)\uff0c\u4e0d\u7ba1\u600e\u4e48\u62bd\uff0c\u968f\u673a\u6570\u90fd\u4f1a&lt;\u6216=50\uff0c\n\u90a3\u4e48\u5f97\u51faresult=c;\n\u56e0\u4e3a\u6837\u672c\u6ca1\u6709\u6539\u53d8\uff0c\u867d\u7136\u53ef\u80fd\u62bd\u53d6\u7684\u968f\u673a\u6570\u4e0d\u6b62\u4e00\u4e2a\uff0c\u4f46\u662f\u6982\u7387\u662f\u4e0d\u53d8\u7684\u3002\n*\/\nfunction get_rand($proArr)\n{\n    $result = '';\n    \/\/\u6982\u7387\u6570\u7ec4\u7684\u603b\u6982\u7387\u7cbe\u5ea6\n    $proSum = array_sum($proArr);\n    \/\/\u6982\u7387\u6570\u7ec4\u5faa\u73af  \n    foreach ($proArr as $key =&gt; $proCur) {\n        $randNum = mt_rand(1, $proSum);\n        if ($randNum &lt;= $proCur) {\n            $result = $key;\n            break;\n        } else {\n            $proSum -= $proCur;\n        }\n    }\n    unset($proArr);\n    return $result;\n}\n\/* \n * \u5956\u9879\u6570\u7ec4 \n * \u662f\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\uff0c\u8bb0\u5f55\u4e86\u6240\u6709\u672c\u6b21\u62bd\u5956\u7684\u5956\u9879\u4fe1\u606f\uff0c \n * \u5176\u4e2did\u8868\u793a\u4e2d\u5956\u7b49\u7ea7\uff0cprize\u8868\u793a\u5956\u54c1\uff0crate\u8868\u793a\u4e2d\u5956\u6982\u7387\u3002 \n * \u6ce8\u610f\u5176\u4e2d\u7684rate\u5fc5\u987b\u4e3a\u6574\u6570\uff0c\u5982\u679crate\u8bbe\u7f6e\u62100\uff0c\u5373\u610f\u5473\u7740\u8be5\u5956\u9879\u62bd\u4e2d\u7684\u51e0\u7387\u662f0\uff0c \n * \u6570\u7ec4\u4e2drate\u7684\u603b\u548c\uff08\u57fa\u6570\uff09\uff0c\u57fa\u6570\u8d8a\u5927\u8d8a\u80fd\u4f53\u73b0\u6982\u7387\u7684\u51c6\u786e\u6027\u3002 \n * \u672c\u4f8b\u4e2drate\u7684\u603b\u548c\u4e3a100\uff0c\u90a3\u4e48MAC\u5bf9\u5e94\u7684 \u4e2d\u5956\u6982\u7387\u5c31\u662f1%\uff0c \n * \u5982\u679crate\u7684\u603b\u548c\u662f10000\uff0c\u90a3\u4e2d\u5956\u6982\u7387\u5c31\u662f\u4e07\u5206\u4e4b\u4e00\u4e86\u3002  \n *\/\n$prize_arr = array(\n    '0' =&gt; array('id' =&gt; 1, 'prize' =&gt; 'MAC', 'rate' =&gt; 1),\n    '1' =&gt; array('id' =&gt; 2, 'prize' =&gt; 'iPhone', 'rate' =&gt; 5),\n    '2' =&gt; array('id' =&gt; 3, 'prize' =&gt; 'iPad', 'rate' =&gt; 10),\n    '3' =&gt; array('id' =&gt; 4, 'prize' =&gt; 'iWatch', 'rate' =&gt; 12),\n    '4' =&gt; array('id' =&gt; 5, 'prize' =&gt; 'iPod', 'rate' =&gt; 22),\n    '5' =&gt; array('id' =&gt; 6, 'prize' =&gt; '\u62b1\u6b49!\u518d\u63a5\u518d\u5389', 'rate' =&gt; 50),\n);\n\/* \n * \u6bcf\u6b21\u524d\u7aef\u9875\u9762\u7684\u8bf7\u6c42\uff0cPHP\u5faa\u73af\u5956\u9879\u8bbe\u7f6e\u6570\u7ec4\uff0c \n * \u901a\u8fc7\u6982\u7387\u8ba1\u7b97\u51fd\u6570get_rand\u83b7\u53d6\u62bd\u4e2d\u7684\u5956\u9879id\u3002 \n * \u5c06\u4e2d\u5956\u5956\u54c1\u4fdd\u5b58\u5728\u6570\u7ec4$res&#91;'yes']\u4e2d\uff0c \n * \u800c\u5269\u4e0b\u7684\u672a\u4e2d\u5956\u7684\u4fe1\u606f\u4fdd\u5b58\u5728$res&#91;'no']\u4e2d\u3002  \n *\/\nforeach ($prize_arr as $key =&gt; $val) {\n    $arr&#91;$val&#91;'id']] = $val&#91;'rate'];\n}\n$rid = get_rand($arr); \/\/\u6839\u636e\u6982\u7387\u83b7\u53d6\u5956\u9879id   \n$res&#91;'yes'] = $prize_arr&#91;$rid - 1]&#91;'prize']; \/\/\u4e2d\u5956\u9879   \nunset($prize_arr&#91;$rid - 1]); \/\/\u5c06\u4e2d\u5956\u9879\u4ece\u6570\u7ec4\u4e2d\u5254\u9664\uff0c\u5269\u4e0b\u672a\u4e2d\u5956\u9879   \nshuffle($prize_arr); \/\/\u6253\u4e71\u6570\u7ec4\u987a\u5e8f   \nfor ($i = 0; $i &lt; count($prize_arr); $i++) {\n    $pr&#91;] = $prize_arr&#91;$i]&#91;'prize'];\n}\n$res&#91;'no'] = $pr;   \/\/\u672a\u4e2d\u5956\u9879  \nprint_r($res);<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[5,7],"class_list":["post-10","post","type-post","status-publish","format-standard","hentry","category-php","tag-php","tag-7"],"_links":{"self":[{"href":"https:\/\/www.72bian.top\/index.php\/wp-json\/wp\/v2\/posts\/10","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.72bian.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.72bian.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.72bian.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.72bian.top\/index.php\/wp-json\/wp\/v2\/comments?post=10"}],"version-history":[{"count":1,"href":"https:\/\/www.72bian.top\/index.php\/wp-json\/wp\/v2\/posts\/10\/revisions"}],"predecessor-version":[{"id":11,"href":"https:\/\/www.72bian.top\/index.php\/wp-json\/wp\/v2\/posts\/10\/revisions\/11"}],"wp:attachment":[{"href":"https:\/\/www.72bian.top\/index.php\/wp-json\/wp\/v2\/media?parent=10"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.72bian.top\/index.php\/wp-json\/wp\/v2\/categories?post=10"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.72bian.top\/index.php\/wp-json\/wp\/v2\/tags?post=10"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}