如何更改数组?

时间:2019-09-20 13:48:37

标签: php arrays laravel

我不知道如何更改或转换数组,或者我在函数中做错了什么?

我得到了这个数组,数字是单词数:

[Food] => 1
[squid] => 1
[next] => 1
[leggings] => 1

最后,我需要一个没有字数的数组:

[0] => Food
[1] => squid
[2] => next
[3] => leggings

那是我的功能:

$theString_1 = "$request->body, $request->titel, $request->articleText";
            // delete special character
            $theString_end = preg_replace("/[^a-zA-Z 0-9 ä ü ö]+/", "", $theString_1 );

            // array of each word in the content separated by 'space'.
            $wordsArray = explode(' ', $theString_end);
            // count words with more than 3 charakters in texts
            $arrayCount = array_count_values(array_filter($wordsArray, function($v) {
                return strlen($v) > 3;
            }));
            //sort array most used word
            arsort($arrayCount);

            //get 4 most used word from the array
            $end = array_slice($arrayCount, 0,4);

1 个答案:

答案 0 :(得分:2)

您有两个选择:

最简单的一个:

$array = array_keys($end);

第二个:

$array = [];
foreach($end as $key=>$value) {
    $array[] = $key;
}

希望有帮助。