好讓目錄中的每個文件應該是簡單的使用glob
然後讀取文件可以用 file_get_contents
完成
/**
* This is how you will add extra rows
*
* $index[] = array(
* 'filename' => 'airlines.txt',
* 'word' => 'JFK',
* 'count' => 3,
* 'all_words_count' => 42
*);
*/
$index = array();
$words = array('jfk', 'car');
foreach($words as $word) {
// All files with a .txt extension
// Alternate way would be "/path/to/dir/*"
foreach (glob("test_files/*.txt") as $filename) {
// Includes the file based on the include_path
$content = file_get_contents($filename, true);
$count = 0;
$totalCount = str_word_count($content);
if(preg_match_all('/' . $word . '/i', $content, $matches)) {
$count = count($matches[0]);
}
// And another item to the list
$index[] = array(
'filename' => $filename,
'word' => $word,
'count' => $count,
'all_words_count' => $totalCount
);
}
}
// Debug and look at the index array,
// make sure it looks the way you want it.
echo '<pre>';
print_r($index);
echo '</pre>';
當我測試上面的代碼,這就是我得到的。
Array
(
[0] => Array
(
[filename] => test_files/airlines.txt
[word] => jfk
[count] => 2
[all_words_count] => 38
)
[1] => Array
(
[filename] => test_files/rentals.txt
[word] => jfk
[count] => 0
[all_words_count] => 47
)
[2] => Array
(
[filename] => test_files/airlines.txt
[word] => car
[count] => 0
[all_words_count] => 38
)
[3] => Array
(
[filename] => test_files/rentals.txt
[word] => car
[count] => 3
[all_words_count] => 47
)
)
我想我已經解決了你的問題:上面的腳本後d此添加到您應該能夠排序計數,從零開始與$sorted
和最高配$sorted_desc
function sorter($a, $b) {
if($a['count'] == $b['count'])
return 0;
return ($a['count'] < $b['count']) ? -1 : 1;
}
// Clone the original list
$sorted = $index;
// Run a custom sort function
uasort($sorted, 'sorter');
// Reverse the array to find the highest first
$sorted_desc = array_reverse($sorted);
// Debug and look at the index array,
// make sure it looks the way you want it.
echo '<h1>Ascending</h1><pre>';
print_r($sorted);
echo '</pre>';
echo '<h1>Descending</h1><pre>';
print_r($sorted_desc);
echo '</pre>';
謝謝!我給了這個嘗試,我只是得到「ArrayArrayArrayArray」等等打印如果我呼應單詞數組......計數工作正常,但我需要將每個單詞存儲在數組中,並能夠檢索它... – 2010-11-04 13:02:41
謝謝,想我即將到達的地方! (忽略以前的評論,我回應,而不是打印......)唯一的事情是,我如何將文件名旁邊的每個單詞和數量存儲在數組中? – 2010-11-04 13:09:52
也即時消除停止/無用的單詞。我有一個單詞陣列即時比較使用array_diff,並且當我將它與末尾的單詞數組進行比較時,似乎沒有任何事情發生,沒有單詞被刪除... – 2010-11-04 13:35:00