如何将每个文本文件的内容存储到关联数组中?

时间:2015-01-31 09:34:03

标签: php arrays

我正在使用相关数组制作搜索引擎现在我想创建一个路径的关联数组,在这个数组中我可以获取键作为文档和值作为内容。

下面是我的代码

    $file= 'D:\\data\\awd_1990_00\\';
        $dictionary = array();
        $docCount = array();

        foreach($collection as $docID => $doc) {
                $terms = explode(' ', $doc);
                $docCount[$docID] = count($terms);

                foreach($terms as $term) {
                        if(!isset($dictionary[$term])) {
                                $dictionary[$term] = array('df' => 0, 'postings' => array());
                        }
                        if(!isset($dictionary[$term]['postings'][$docID])) {
                                $dictionary[$term]['df']++;
                                $dictionary[$term]['postings'][$docID] = array('tf' => 0);
                        }

                        $dictionary[$term]['postings'][$docID]['tf']++;
                }
        }

        return array('docCount' => $docCount, 'dictionary' => $dictionary);
}
?>

如您所见$collection是一个关联数组,我想帮助我

1 个答案:

答案 0 :(得分:0)

尝试使用此功能:

<?php

    function extractDocuments($p_dir) {

       if(!is_dir($p_dir)) {  //Check if $p_dir is a valid directory

        //Throw exception or return FALSE  
       }

        $path=$p_dir;
        $dir=scandir($path); //Load directory contents
        $collection=array();

        foreach($dir as $file) { //Go through directory
          if($file==".." || $file==".") {continue;} //Exclude parent directory and self
          $collection[$file]=file_get_contents($path.$file); //Load file contents and save
        }

        return $collection;
    }

?>