我可以使用兰德('待定','成功','失败','退款')

时间:2014-05-08 12:20:34

标签: php mysql

我是否可以将payment_status = rand('pending','success','failed','refunded')用于以下查询,或者任何人都可以通过以下查询帮助我解决我的错误。

for ($i=0;$i<100000;$i++)   
{ 
$payment_status = rand('pending','success','failed','refunded');    
$sql = 'INSERT INTO `aa_kkkk` (`id`, `user_id`, `biz_id`, `filing_year`, `filing_month`, `final_return`, `consent_disclosure`, `is_third_party_designee`, `amended_month`, `earliest_date`, `latest_date`, `tax_year_end_month`, `address_change`, `form_type`, `submission_id`, `payment_status`, `transaction_id`, `active`, `xml_submitted`, `date_user_submitted`, `date_xml_sent`, `date_last_ack_attempt`, `date_acknowledged`, `created_date`, `modified_date`, `next_submission_date`, `irs_approved`, `ack_received`, `sch1_received`, `sch1_path`, `user_completed`, `consent_to_submit`, `error_code`, `error_description`, `error_type`, `modifiedby_admin`, `data_masked`, `form_pdf_path`) VALUES
(Null, ':user_id', ':biz_id', ':filing_year', "c2aaf5544415889e720f042b04551c97'.$i.'", "c9c396be60e066ae92b978c08a3fca0a'.$i.'", "32b20aa703b1128d448321b83fe57d70'.$i.'", "0", "32b20aa703b1128d448321b83fe57d70'.$i.'", "32b20aa703b1128d448321b83fe57d70'.$i.'", "32b20aa703b1128d448321b83fe57d70'.$i.'", "32b20aa703b1128d448321b83fe57d70'.$i.'", "32b20aa703b1128d448321b83fe57d70'.$i.'",  "84843320140520033138'.$i.'", ':payment_status', "", ':transaction_id', ':active', "2014-02-21 10:31:35", "2014-02-21 03:31:42", "2014-02-21 03:32:00", "2014-02-21 03:32:00", "2014-02-21 03:11:15", "2014-04-18 11:12:39", NULL, ':irs_approved', ':ack_received', ':sch1_received', "84843320140520033138.pdf'.$i.'", 1, 1, "", "", "", 0, 0, "pf2290_1_84843320140520033138.pdf'.$i.'")';

4 个答案:

答案 0 :(得分:4)

echo rand('pending','success','failed','refunded');

警告:rand()需要2个参数,4个

文档:rand()

所以,不,你不能。不是那样的。

这将有效:

$values = array('pending','success','failed','refunded');
$payment_status = $values[rand(0, sizeof($values)-1)];

或者使用array_rand()(必须爱PHP ......一切功能......):

$values = array('pending','success','failed','refunded');
$payment_status = $values[array_rand($values)];

为了好玩:您还可以发挥创意并使用shuffle

$values = array('pending','success','failed','refunded');
shuffle($values);
$payment_status = $values[0];

虽然它并不像它上面的两个选项那样高效,因为它会首先对数组进行洗牌,然后返回其中的第一个元素,而不是仅从列表中选择一个随机元素并返回它。

答案 1 :(得分:4)

$rand = $array[array_rand($array = ['pending','success','failed','refunded'])];

或者:

$array = ['pending','success','failed','refunded'];
$rand  = $array[rand(0, count($array)-1)];

或......

答案 2 :(得分:1)

您也可以这样做。

$input = array('pending','success','failed','refunded'); 
echo $input[array_rand($input)];

我更喜欢这种方法,因为通过使用它,您不必指定数组的大小。在某些情况下可能不知道。

答案 3 :(得分:0)

您可以使用以下

$k = array_rand($array);
$v = $array[$k];

请参阅文档here

享受:)