Skip to content

Commit aec8e4f

Browse files
committed
aa
1 parent 091a5a5 commit aec8e4f

File tree

1 file changed

+77
-2
lines changed

1 file changed

+77
-2
lines changed

function.md

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55
```
66

77
[数字转为中文大写](#zhuangdaxie)
8-
9-
8+
[导出execl表格](#execl)
9+
[获取本月月份 第一天和最后一天](#fristdayendday)
10+
[将json串解析成中文](#json-china)
11+
[php数组根据某键值,把相同键值的合并最终生成一个新的二维数组](#newarray)
12+
[利用PHP 把某个数值随机分配成几份](#numincise)
1013

1114
<h4 id="zhuangdaxie">数字转为中文大写</h4>
1215
```
@@ -64,3 +67,75 @@ public function get_amount($num){
6467
}
6568
}
6669
```
70+
71+
72+
<h4 id="execl">导出execl表格</h4>
73+
```
74+
$filename = '文档名称'. ' .csv/.xls';
75+
header("Cache-Control: public" );
76+
header("Pragma: public" );
77+
header("Content-type:application/vnd.ms-excel");
78+
header("Content-Disposition:attachment;filename={$filename}");
79+
header('Content-Type:APPLICATION/OCTET-STREAM');
80+
81+
/*添加csv表格标题*/
82+
$titleList = array('表格标题1', '表格标题2', '表格标题3', '表格标题4', '表格标题5', '表格标题6');
83+
$titleString = implode(',', $titleList) . "\r\n"; /*(\r\n) 表示换行*/
84+
echo mb_convert_encoding($titleString, 'gbk', 'utf-8'); /*以utf-8的格式输出*/
85+
86+
foreach(){
87+
$dataList = array(); /*定义新数组*/
88+
$dataList[] = '数据';
89+
$dataList[] = '数据';
90+
$dataList[] = '数据';
91+
$dataList[] = '数据';
92+
$dataList[] = '数据';
93+
$dataList[] = '数据';
94+
95+
$activityString = implode(',', $dataList) . "\r\n";
96+
echo mb_convert_encoding($activityString, 'gbk', 'utf-8'); /*输出每一行数据*/
97+
}
98+
```
99+
100+
101+
<h4 id="fristdayendday">获取本月月份第一天和最后一天</h4>
102+
```
103+
$date = date('Y-m-d');
104+
$firstday = date('Y-m-01', strtotime($date));
105+
$lastday = date('Y-m-d', strtotime("$firstday +1 month -1 day"));
106+
```
107+
108+
109+
110+
<h4 id="json-china">将json串解析成中文</h4>
111+
```
112+
$str = preg_replace("#\\\u([0-9a-f]+)#ie", "iconv('UCS-2', 'UTF-8', pack('H4', '\\1'))", $str);
113+
114+
实例:
115+
$str = preg_replace("#\\\u([0-9a-f]+)#ie", "iconv('UCS-2', 'UTF-8', pack('H4', '\\1'))", '\u4ea4\u6613\u5bc6\u7801\u9519\u8bef');
116+
```
117+
118+
119+
120+
<h4 id="newarray">php数组根据某键值,把相同键值的合并最终生成一个新的二维数组</h4>
121+
```
122+
foreach($sourceArray as $k=>$v) {
123+
$result[$v["sendto"]][] = $v; // sendto 根据你想要的相同的需要合并的键值
124+
}
125+
```
126+
127+
128+
129+
<h4 id="numincise">利用PHP 把某个数值 随机分配成几份</h4>
130+
```
131+
$total = 10; //数值总额
132+
$num = 8; // 分成8份
133+
$min = 0.005; //最小数值
134+
for ($i = 1; $i < $num; $i++) {
135+
$safe_total = ($total - ($num - $i) * $min) / ($num - $i); //随机安全上限
136+
$money = mt_rand($min * 100, $safe_total * 100) / 100;
137+
$total = $total - $money;
138+
echo '第' . $i . '个数值:' . $money ;
139+
}
140+
echo '第' . $num . '个数值:' . $total;// 最后一份数值
141+
```

0 commit comments

Comments
 (0)