大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
function getDivideNumber($number, $total, $index = 2) {
专注于为中小企业提供网站设计、成都网站制作服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业下花园免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了千余家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。
// 取平均数
$divide_number = floor($number / $total * pow(10, $index)) / pow(10, $index);
$divide_number = number_format($divide_number, $index, '.', '');
// 获取最后一个数字
$last_number = $number - $divide_number * ($total - 1);
$last_number = number_format($last_number, $index, '.', '');
// 拼装平分后的数据返回
$number_str = str_repeat($divide_number . ',', $total - 1) . $last_number;
return explode(',', $number_str);
}
$array = getDivideNumber(120, 3, $index = 0);
得到平均分配的数字数组,用遍历后入库
这个是模板机制,可能自己写的,你参照一下smarty就知道怎么回事了,不过DEDE是自己的模板机制
str_split函数能实现你这样的功能,把字符串按长度分割为数组,例如:
?php
$str = "Hello Friend";
$arr1 = str_split($str);
$arr2 = str_split($str, 3);
print_r($arr1);
print_r($arr2);
?
代码输出的结果如下:
Array
(
[0] = H
[1] = e
[2] = l
[3] = l
[4] = o
[5] =
[6] = F
[7] = r
[8] = i
[9] = e
[10] = n
[11] = d
)
Array
(
[0] = Hel
[1] = lo
[2] = Fri
[3] = end
)
?php$data = array("4,0,9#1_1", "4,5,5#1_1","4,5,1#1_1", "7,2,4#1_1", "4,4,3#1_1", "8,8,0#2_1","2,2,9#2_1","0,0,6#2_1", "0,0,7#2_1","3,3,8#2_1" );$result1 = array();
$result2 = array();
foreach($data as $key=$value)
{
$str1 = '#1_1';
$str2 = '#2_1'; if(strpos($value,$str1))
{
$tmp = str_replace($str1,'',$value);
$result1[] = $tmp;
}
else if(strpos($value,$str2))
{
$tmp = str_replace($str2,'',$value);
$result2[] = $tmp;
}
}
print_r($result1);
print_r($result2);
?
结果:Array ( [0] = 4,0,9 [1] = 4,5,5 [2] = 4,5,1 [3] = 7,2,4 [4] = 4,4,3 ) Array ( [0] = 8,8,0 [1] = 2,2,9 [2] = 0,0,6 [3] = 0,0,7 [4] = 3,3,8 )楼上大哥的是对的~~