======[PHP] Gewichtet Wörter zählen in einem mehrdimensionalem Array====== 'Seite 1', 'text' => 'Ich diesem Text kommt das Wort Startseite vor ;)', 'pfad' => 'http://localhost/test/error_404.html'), array( 'title' => 'Startseite', 'text' => 'Hier komm das Wort nicht vor aber im Titel und test mit ö und Ä', 'pfad' => 'http://localhost/test/index.html') ); //Gewichtung $gewichtung = array('title' => 5, 'text' => 2, 'pfad' => 1); $wordList = array(); //für jede [key]=>[value] Kombination die Funktion countWords aufrufen array_walk_recursive($data, 'countWords'); arsort($wordList); foreach($wordList as $word => $count) echo "{$word}: {$count}
"; function countWords($item, $key){ //Die Wörter auflisten und zählen -> array([wort]=>anzahl) $thisWords = array_count_values(str_word_count($item, 1)); //jeder Eintrag der gefundenen Wörter mit dem Faktor multipliziert der wordList hinzufügen array_walk($thisWords, 'addToWordList', $GLOBALS['gewichtung'][$key]); } function addToWordList($count, $word, $factor){ $GLOBALS['wordList'][$word] = $GLOBALS['wordList'][$word]+($count*$factor); } ?>
---- Kategorien: [[CatPhp]]