我有一个类似这样的数组
Array
(
[0] => section 1
[1] => xyz
[2] => xyz
[3] => xyz
[4] => ====================================================================================================
[5] => section 2
[6] => abc
[7] => abc
[8] => ====================================================================================================
)
所以我想把这个数组转换成一个二维数组,其中==================作为分隔符
可能是这样的吗?
Array
(
[0] => Array
(
[0] => section 1
[1] => xyz
[2] => xyz
[3] => xyz
)
[1] => Array
(
[0] => section 2
[1] => abc
[2] => abc
)
)
试图使用爆炸方法,但似乎没有返回我真正想要的东西
答案 0 :(得分:5)
explode()
使用字符串。您可以使用常见的foreach
- 循环:
$result = [];
$index = 0;
foreach($array as $value){
if(preg_match('/^\=+$/', $value)){
$index++;
} else {
$result[$index][] = $value;
}
}
print_r($result);
示例:
<?php
header('Content-Type: text/plain; charset=utf-8');
$array = [
1,
2,
'section',
'=============',
'test',
'test2',
null
];
$result = [];
$index = 0;
foreach($array as $value){
if(preg_match('/^\=+$/', $value)){
$index++;
} else {
$result[$index][] = $value;
}
}
print_r($result);
?>
输出:
Array
(
[0] => Array
(
[0] => 1
[1] => 2
[2] => section
)
[1] => Array
(
[0] => test
[1] => test2
[2] =>
)
)