【问题标题】:Explode a semicolon string and chunk the parts out of it分解一个分号字符串并将其中的部分分块
【发布时间】:2016-02-29 14:57:26
【问题描述】:

我拥有的是

$string = 'one;two;three;four;five;six;seven;eight;nine;ten';

我需要的是:

$array = [
   ['one', 'two', 'three'],
   ['four', 'five', 'six'],
   ['seven', 'eight', 'nine'],
   ['ten'],
];

基本上字符串可以有无限的值。

【问题讨论】:

  • 总是三人一组?
  • 你不知道 PHP 有一个documentation 吗?现在你可以了。
  • Argg...我不知道我是怎么错过的。

标签: php arrays regex split php-5.5


【解决方案1】:

可以通过array_chunk函数完成:

$string = 'one;two;three;four;five;six;seven;eight;nine;ten';
$array = array_chunk(explode(';', $string), 3);

【讨论】:

  • 我喜欢问题标题是变相的答案。
猜你喜欢
  • 2017-01-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-14
  • 1970-01-01
相关资源
最近更新 更多