将平面句子数组拆分为二维单词数组

问题描述 投票:0回答:1

我有一个字符串数组,它们是句子,我需要将其拆分为多个单词数组。我尝试使用

array_chunk()
,但我必须执行两次才能获得我想要的内容,并且我无法再次访问分割数组的每个单独元素来
array_chunk()

抱歉,如果这令人困惑,基本上我想要的是;

$arr = [
    "this is a",
    "sentence and",
    "this is stackoverflow"
];

分为:

$arr1 = ["this", "is", "a"];
$arr2 = ["sentence", "and"];
$arr3 = ["this", "is", "stackoverflow"];

这是我尝试过的

$chunk = (array_chunk($userinf, 1));
//$chunks = (array_chunk($chunk, 1));
//print_r($chunks);
for ($i = 0; $i < count($chunks); $i++) {
    do something
}
php split cpu-word
1个回答
1
投票

只需遍历现有数组并将值重新分配为新数组即可。像这样...

$arr = ["this is a", "sentence and", "this is stackoverflow"];

foreach ($arr as $key => $val) {

    $arr[$key] = explode(" ", $val);

}

结果是...

$arr = [["this","is","a"],["sentence","and"],["this","is","stackoverflow"]]
© www.soinside.com 2019 - 2024. All rights reserved.