将字符串转换为块数为4的数组

时间:2016-04-15 22:23:48

标签: php arrays string

我有以下字符串:

$my_string = 'one|two|three|four|five|six|seven|eight|nine|ten|eleven|twelve|thirteen|fourteen|fifteen';

我想将$my_string转换为块数为4的数组,如下所示:

$my_array = Array (
array(
    "one",
    "two",
    "three",
    "four"
),
Array (
    "five",
    "six",
    "seven",
    "eight"
),
Array (
    "nine",
    "ten",
    "eleven",
    "twelve"
),
Array (
    "thirteen",
    "fourteen",
    "fifteen"
)

怎么做?

我认为可以使用preg_split()功能完成吗?但我不知道该怎么做。

1 个答案:

答案 0 :(得分:0)

一行简单的代码......

$array = array_chunk(explode('|', $my_string), 4);