note 46328 modified in function.array-unique by aidan
| From: | aidan@php.net | Date: | Thu, 07 Oct 2004 10:24:35 +0000 |
| Subject: | note 46328 modified in function.array-unique by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-78117@lists.php.net to get a copy of this message | ||
This script will unique the array and count the times appearance.
find more information at http://php.scmtv.com/
--was--
<?
function count_words($string = "")
{
/**
* @return array
* @desc this script will unique the array and count the times appearance
* @author vary
* @email varyer at gmail dot com
* @for more php scripts information, please visit http://php.scmtv.com/
*/
$words = str_word_count($string, 1);
$array = array(
'keys' => array(),
'nums' => array()
);
foreach ($words as $key=>$val) {
if (in_array($val,$array['keys'])) {
$offset = array_search($val,$array['keys']);
$array['nums'][$offset]++;
} else {
$array['keys'][$key] = $val;
$array['nums'][$key] = 1;
}
}
array_multisort($array['nums'], SORT_DESC,SORT_NUMERIC,$array['keys']);
return $array;
}
###########################################################
$string = "XHTML has only minor changes from HTML. So if your pages are HTML 4.01 compliant,
then your ahead of the game already. Changing to XHTML has some of the following syntax
requirements: attribute names must be in lower case, attribute values must be quoted, attribute
minimization is forbidden, the id attribute replaces the name attribute and the XHTML DTD defines
mandatory elements";
$array = count_words($string);
print_r($array);
?>
print:
Content-type: text/html
X-Powered-By: PHP/4.3.6
Array
(
[keys] => Array
(
[0] => attribute
[1] => the
[2] => XHTML
[3] => HTML
[4] => be
[5] => has
[6] => must
[7] => of
[8] => your
[9] => Changing
[10] => DTD
[11] => So
[12] => ahead
[13] => already
[14] => and
[15] => are
[16] => case
[17] => changes
[18] => compliant
[19] => defines
[20] => elements
[21] => following
[22] => forbidden
[23] => from
[24] => game
[25] => id
[26] => if
[27] => in
[28] => is
[29] => lower
[30] => mandatory
[31] => minimization
[32] => minor
[33] => name
[34] => names
[35] => only
[36] => pages
[37] => quoted
[38] => replaces
[39] => requirements
[40] => some
[41] => syntax
[42] => then
[43] => to
[44] => values
)
[nums] => Array
(
[0] => 5
[1] => 5
[2] => 3
[3] => 2
[4] => 2
[5] => 2
[6] => 2
[7] => 2
[8] => 2
[9] => 1
[10] => 1
[11] => 1
[12] => 1
[13] => 1
[14] => 1
[15] => 1
[16] => 1
[17] => 1
[18] => 1
[19] => 1
[20] => 1
[21] => 1
[22] => 1
[23] => 1
[24] => 1
[25] => 1
[26] => 1
[27] => 1
[28] => 1
[29] => 1
[30] => 1
[31] => 1
[32] => 1
[33] => 1
[34] => 1
[35] => 1
[36] => 1
[37] => 1
[38] => 1
[39] => 1
[40] => 1
[41] => 1
[42] => 1
[43] => 1
[44] => 1
)
)
find more information at http://php.scmtv.com/
http://php.net/manual/en/function.array-unique.php