2 回答
TA贡献1846条经验 获得超7个赞
首先,您需要将逗号分隔的字符串转换为数组。然后你可以使用array_chunk()函数。
$myString = "5.1,3.5,Red,4.9,3,Blue,4.7,3.2,Red,4.6,3.1,Red,5,3.6,Red";
$explodedStringToArray = explode(',', $myString);
$chunked_array = array_chunk($explodedStringToArray, 3);
print_r($chunked_array);
这将产生:
Array
(
[0] => Array
(
[0] => 5.1
[1] => 3.5
[2] => Red
)
[1] => Array
(
[0] => 4.9
[1] => 3
[2] => Blue
)
[2] => Array
(
[0] => 4.7
[1] => 3.2
[2] => Red
)
[3] => Array
(
[0] => 4.6
[1] => 3.1
[2] => Red
)
[4] => Array
(
[0] => 5
[1] => 3.6
[2] => Red
)
)
TA贡献1811条经验 获得超4个赞
explode()您可以在字符串上使用,然后array_chunk()使用 explode 函数对我们拥有的数组进行分块,请记住检查分块大小
<?php
$myString = "5.1,3.5,Red,4.9,3,Blue,4.7,3.2,Red,4.6,3.1,Red,5,3.6,Red"; //and so on
$arr = explode(",", $myString);
$chunks = array_chunk($Arr, 3);
print_r($chunks);
- 2 回答
- 0 关注
- 180 浏览
添加回答
举报