zkanoca
zkanoca

Reputation: 9918

How to change array key value in PHP

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..."

Upvotes: 0

Views: 66

Answers (1)

svvac
svvac

Reputation: 6124

Change

$fulltext_course_files[] = array_count_values($swc);

To

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

Upvotes: 1

Related Questions