如何在PHP中更改数组键值

时间:2014-06-20 13:38:23

标签: php arrays file

我正在尝试提取位于文件夹中的html文档的字数。

<?php

$rii = new RecursiveIteratorIterator(new RecursiveDirectoryIterator('fulltext/course'));
$fulltext_course_files = array();

foreach ($rii as $f) {
    if ($f->isDir()) {
        continue;
    } else {

        $st = strip_tags(strtolower(file_get_contents($f)));
        $swc = str_word_count($st, 1);
        $fulltext_course_files[] = array_count_values($swc);
    }
}


print_r($fulltext_course_files);
?>

此代码显示每个文档中的单词和频率。但是数组索引是数字,我希望它是文件名。

print_r($fulltext_course_files);

显示了一些

Array ( [0] => Array ( [cs] => 7 [home] => 1 [page] => 1 [systems] => 2 [programming] => 1 [and] => 5 [operating] => 2 [practicum] => 1 ....

但我想要

Array ( [0] => Array ( [cs] => 7 [home]...

Array ( ["file1.html"] => Array ( [cs] => 7 [home]...

我试过了

$fulltext_course_files[$f] = array_count_values($swc);

但我得到了&#34; 警告:非法抵消类型... &#34;

1 个答案:

答案 0 :(得分:1)

更改

$fulltext_course_files[] = array_count_values($swc);

$fulltext_course_files[$f->getFilename()] = array_count_values($swc);