?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();
创新互联专注为客户提供全方位的互联网综合服务,包含不限于成都网站设计、做网站、成都外贸网站建设公司、屯溪网络推广、微信小程序、屯溪网络营销、屯溪企业策划、屯溪品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联为所有大学生创业者提供屯溪建站搭建服务,24小时服务热线:028-86922220,官方网址:www.cdcxhl.com
$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 )楼上大哥的是对的~~
愚见:
用函数explode(",",$hq_str_sh601006)
能把字符串按照逗号分开。可以直接赋值给一个数组变量。
如:$hq_str_arr=explode(",",$hq_str_sh601006);
然后你自己可以从数组中按照你获取的顺序给数组中相应的元素赋值给数据库的对应字段。
希望有帮助。
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
)
大致思路
1)取份数的平均值
2)根据允许的各份数间最大差值,动态调整随机数范围,形成各份的数量
3)最后一份的数量由前面已经分出的决定,以保证各份的总和为指定值
代码如下:
?php
$total = 100; //待划分的数字
$div = 5; //分成的份数
$area = 10; //各份数间允许的最大差值
$average = round($total / $div);
$sum = 0;
$result = array_fill( 1, $div, 0 );
for( $i = 1; $i $div; $i++ ){
//根据已产生的随机数情况,调整新随机数范围,以保证各份间差值在指定范围内
if( $sum 0 ){
$max = 0;
$min = 0 - round( $area / 2 );
}elseif( $sum 0 ){
$min = 0;
$max = round( $area / 2 );
}else{
$max = round( $area / 2 );
$min = 0 - round( $area / 2 );
}
//产生各份的份额
$random = rand( $min, $max );
$sum += $random;
$result[$i] = $average + $random;
}
//最后一份的份额由前面的结果决定,以保证各份的总和为指定值
$result[$div] = $average - $sum;
//结果呈现
echo '划分情况:br';
foreach( $result as $temp ){
echo $temp, 'br';
}
echo '总和:', array_sum( $result );
exit;
?
?php
$str = "ABC'; F(ZZZ), KSE KKS(YYY), PSX-D.M(XXX)";
$arr = explode(",",$str);//分割成数组
foreach($arr as $value){//用foreach循环,也可以用for循环
$out = array();//储存匹配的数组
$title = "";//储存title部分
$text = "";//储存链接显示文本
//先把括号里的内容和括号外的内容匹配到数组$out
preg_match("|(.+)\((.+)\)|",$value,$out);
//括号里的内容就是title
$title = $out[2];
//把括号外的内容中所有符号替换成-(这里去了一下首尾空格)
$text = preg_replace("|(\W+)|","-",trim($out[1]));
//OK了,输出
echo "a href=\"#\" title=\"{$title}\" /{$text}/a,";
}
/**
最后输出:
a href="#" title="ZZZ" /ABC-F/a,a href="#" title="YYY" /KSE-KKS/a,a href="#" title="XXX" /PSX-D-M/a,
*/
$data1 = array("a2"-"类一","V4"-“类二”);
$data2 = array();
foreach($data1 as $key=$value) {
$data2[$value] = $data2[$value].' '.$key;
}