扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
explode
在滨江等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供网站设计制作、成都网站设计 网站设计制作按需求定制制作,公司网站建设,企业网站建设,成都品牌网站建设,网络营销推广,成都外贸网站制作,滨江网站建设费用合理。
使用一个字符串分割另一个字符串
$str = "11,22,33";$arr = explode(",",$str);
print_r($arr);Array(
[0] = 11
[1] = 22
[2] = 33)12345678910
implode
将一个一维数组的值转化为字符串
$arr = [11,22,33];$str = implode("#",$arr);echo $str;11#22#3312345
split
用正则表达式将字符串分割到数组中
preg_split()函数使用了Perl兼容正则表达式语法,通常是比split()更快的替代方案。如果不需要正则表达式的威力,则使用explode()更快,这样就不会招致正则表达式引擎的浪费。
该函数在PHP7中已被废弃
$str = "123dqaw76eqwv8902fwer12356";$arr = split("[a-z]+", $str);
print_r($arr);Array(
[0] = 123
[1] = 76
[2] = 8902
[3] = 12356)1234567891011
preg_split
通过一个正则表达式分隔字符串
如果你不需要正则表达式功能,可以有更快(并且更简单)的选择比如 explode() 或 str_split()。
如果没有成功匹配,将会返回一个数组,包含了单个元素,即输入的字符串。
$str = "123dqaw76eqwv8902fwer12356";$arr = preg_split("/[a-z]+/i", $str);
print_r($arr);Array(
[0] = 123
[1] = 76
[2] = 8902
[3] = 12356)1234567891011
str_split
将字符串转换为数组
$str = "hello world";
print_r(str_split($str));
print_r(str_split($str, 3));Array(
[0] = h
[1] = e
[2] = l
[3] = l
[4] = o
[5] =
[6] = w
[7] = o
[8] = r
[9] = l
[10] = d
)Array(
[0] = hel
[1] = lo
[2] = wor
[3] = ld
)12345678910111213141516171819202122232425
array_chunk
将一个数组分割成多个
$input_array = array('a', 'b', 'c', 'd', 'e');
print_r(array_chunk($input_array, 2));
print_r(array_chunk($input_array, 2, true));Array(
[0] = Array
(
[0] = a
[1] = b
)
[1] = Array
(
[0] = c
[1] = d
)
[2] = Array
(
[0] = e
)
)Array(
[0] = Array
(
[0] = a
[1] = b
)
[1] = Array
(
[2] = c
[3] = d
)
[2] = Array
(
[4] = e
)
)1234567891011121314151617181920212223242526272829303132333435363738394041424344
array_combine
创建一个数组,用一个数组的值作为其键名,另一个数组的值作为其值
$a = array('green', 'red', 'yellow');$b = array('avocado', 'apple', 'banana');$c = array_combine($a, $b);
print_r($c);Array(
[green] = avocado
[red] = apple
[yellow] = banana
)1234567891011
array_merge
合并一个或多个数组。一个数组中的值附加在前一个数组的后面,返回作为结果的数组。
别忘了数字键名将会被重新编号!
$array1 = array("color" = "red", 2, 4);$array2 = array("a", "b", "color" = "green", "shape" = "trapezoid", 4);$result = array_merge($array1, $array2);
print_r($result);Array(
[color] = red
[0] = 2
[1] = 4)Array(
[0] = a
[1] = b
[color] = green
[shape] = trapezoid
[2] = 4)Array(
[color] = green
[0] = 2
[1] = 4
[2] = a
[3] = b
[shape] = trapezoid
[4] = 4)1234567891011121314151617181920212223242526272829
如果你想完全保留原有数组并只想新的数组附加到后面,用 + 运算符。如果两个被合并的数组含有相同的key,则保留第一个,忽略后边的。
$array1 = array(0 = 'zero_a', 2 = 'two_a', 3 = 'three_a');$array2 = array(1 = 'one_b', 3 = 'three_b', 4 = 'four_b');$result = $array1 + $array2;
print_r($result);Array(
[0] = zero_a
[2] = two_a
[3] = three_a
[1] = one_b
[4] = four_b
)12345678910111213
array_merge_recursive
递归地合并一个或多个数组
如果输入的数组中有相同的字符串键名,则这些值会被合并到一个数组中去,这将递归下去,因此如果一个值本身是一个数组,本函数将按照相应的条目把它合并为另一个数组。然而,如果数组具有相同的数组键名,后一个值将不会覆盖原来的值,而是附加到后面。
$ar1 = array("color" = array("favorite" = "red"), 5);$ar2 = array(10, "color" = array("favorite" = "green", "blue"));$result = array_merge_recursive($ar1, $ar2);
print_r($ar1);
print_r($ar2);
print_r($result);Array(
[color] = Array
(
[favorite] = red
)
[0] = 5)Array(
[0] = 10
[color] = Array
(
[favorite] = green
[0] = blue
)
)Array(
[color] = Array
(
[favorite] = Array
(
[0] = red
[1] = green
)
[0] = blue
)
[0] = 5
[1] = 10)123456789101112131415161718192021222324252627282930313233343536373839404142
compact
建立一个数组,包括变量名和它们的值
对每个参数,compact() 在当前的符号表中查找该变量名并将它添加到输出的数组中,变量名成为键名而变量的内容成为该键的值。
任何没有变量名与之对应的字符串都被略过。
$city = "San Francisco";$state = "CA";$event = "SIGGRAPH";$location_vars = array("city", "state");$result = compact("event", "nothing_here", $location_vars);
print_r($result);Array(
[event] = SIGGRAPH
[city] = San Francisco
[state] = CA
)12345678910111213
extract
从数组中将变量导入到当前的符号表
检查每个键名看是否可以作为一个合法的变量名,同时也检查和符号表中已有的变量名的冲突。
返回成功导入到符号表中的变量数目。
$var_array = [ "color" = "blue", "size" = "medium", "shape" = "sphere"];
extract($var_array);echo "$color, $size, $shape\n";
blue, medium, sphere123456789
参考
PHP手册
php数组分组可以使用函数array_chunk,按照给定的规则进行遍历,如果数组的num值大于8,就整除8,获取整除的数据,然后按照整除数进行分组分块即可。
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
)
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流