0

I am trying to extract word counts of html documents located in a folder.

<?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);
?>

this code displays words and frequencies in each document. But array index is numeric which I want it to be filename.

print_r($fulltext_course_files);

shows some like that

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

But I want

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

to be

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

I have tried

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

but I got "Warning: Illegal offset type..."

1 Answer 1

1

Change

$fulltext_course_files[] = array_count_values($swc);

To

$fulltext_course_files[$f->getFilename()] = array_count_values($swc);
Sign up to request clarification or add additional context in comments.

1 Comment

I skipped that you were in fact using a RecursiveIteratorIterator. Corrected the answer to retrieve filename from the object

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.