1、多个字段多重排序
function sortArrByManyField(){
$args = func_get_args(); // 获取函数的参数的数组
if(empty($args)){
return null;
}
$arr = array_shift($args);
if(!is_array($arr)){
return array();
}
foreach($args as $key => $field){
if(is_string($field)){
$temp = array();
foreach($arr as $index=> $val){
$temp[$index] = $val[$field];
}
$args[$key] = $temp;
}
}
$args[] = &$arr;//引用值
call_user_func_array('array_multisort',$args);
return array_pop($args);
}
$userActionGoodsList = $this->sortArrByManyField($userActionGoodsNlist,'num',SORT_DESC,'time_span',SORT_DESC,'id',SORT_DESC);
2、php抽奖逻辑实现
{
"pname": "米达料小葫芦", // 抽奖商品名称
"ptype": "product", // 抽奖商品类型
"pvalue": "29", // 抽奖商品值
"prate": "4", // 抽奖概率
"img": "https://mallssy.oss-cn-shenzhen.aliyuncs.com/home/store/goods/2/oss_2_2019040417473329380.png" // 抽奖商品图片
},
public function getLotteryGoods(){
$l_id = 1;
$lottery_model = new appcommonmodelLotteryrule();
$linfo = $lottery_model->getOneById($l_id);
$lottery_json_list = $linfo['lottery_data'];
$prize_arr = json_decode($lottery_json_list,true);
$num = 1;
foreach ($prize_arr as $key => $val) {
$arr[$num] = $val['prate'];
$num++;
}
$rid = $this->get_rand($arr); //根据概率获取奖项id
$res = $prize_arr[$rid-1]; //中奖项
return $res;
}
function get_rand($proArr) {
$result = '';
//概率数组的总概率精度
$proSum = array_sum($proArr);
//概率数组循环
foreach ($proArr as $key => $proCur) {
$randNum = mt_rand(1, $proSum);
if ($randNum <= $proCur) {
$result = $key;
break;
} else {
$proSum -= $proCur;
}
}
unset ($proArr);
return $result;
}
3、文件加锁
public function fileLock(){
//图片保存到其它地方
$lock_dir = __DIR__."/../../../public/uploads/fileLock";
//4、检查目录是否存在
if (!is_dir($lock_dir)) {
mkdir($lock_dir,0777,true);
}
$fp = fopen($lock_dir.'/test.txt', "w+");
if (flock($fp, LOCK_EX)) { // 进行排它型锁定
$content = date('Y-m-d H:i:s').'插入的'.PHP_EOL;
fwrite($fp, $content);
flock($fp, LOCK_UN); // 释放锁定
} else {
echo "Couldn't lock the file !";
}
fclose($fp);
}
后续还会更新,谢谢各位亲关注!