例如:
$string = '1,2,3,4,5,6,7,8,9,10,11,12,13,14,15';
然后像分页一样展开它,例如每行 8 个值将导致两个 2 页:
$arr = p_explode(',', $string, 8);
array(
0 => "1,2,3,4,5,6,7,8"
1 => "9,10,11,12,13,14,15"
)
或者,例如分解为 5 个值,每行将导致 3 页:
$arr = p_explode(',',$string,5);
array(
0 => "1,2,3,4,5"
1 => "6,7,8,9,10"
2 => "11,12,13,14,15"
)
p_explode 将是:
p_explode(string_delimiter, string_string, int_number_values_each_page)
是否可以?
最佳答案
使用 array_chunk()
和 explode()
PHP 的函数:
$elementsPerPage = 5;
$arrayOfPages = array_chunk(explode(',', $string), $elementsPerPage);
print_r($arrayOfPages);
输出:
Array
(
[0] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
)
[1] => Array
(
[0] => 6
[1] => 7
[2] => 8
[3] => 9
[4] => 10
)
[2] => Array
(
[0] => 11
[1] => 12
[2] => 13
[3] => 14
[4] => 15
)
)
array_chunk()
将数组拆分为块。explode()
按字符串拆分字符串,在这种情况下,创建一个数组,该数组由 $string
中包含的所有数字除以 ,
组成。关于php - 是否可以像分页一样 explode 字符串?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48423200/