开发者

How can we get unique elements from any ORDER BY DECREASING OR INCREASING

开发者 https://www.devze.com 2023-02-01 07:55 出处:网络
Code given below is taken from the stackoverflow.com !!! Can anyone tell me how to get the array elements order by decreaseing or increasing !! plz help me !!! Thanks in advance

Code given below is taken from the stackoverflow.com !!! Can anyone tell me how to get the array elements order by decreaseing or increasing !! plz help me !!! Thanks in advance

$contents = file_get_contents($htmlurl);

// Get rid of style, script etc
$search = array('@<script[^>]*?>.*?</script>@si',  // Strip out javascript
           '@<head>.*?</head>@siU',            // Lose the head section
           '@<style[^>]*?>.*?<开发者_如何转开发/style>@siU',    // Strip style tags properly
           '@<![\s\S]*?--[ \t\n\r]*>@'         // Strip multi-line comments including CDATA
);

$contents = preg_replace($search, '', $contents); 

$result = array_count_values(
              str_word_count(
                  strip_tags($contents), 1
                  )
              );

print_r($result);


You need PHP's sort function. I won't duplicate the manual here.


After stripping the tags, you get an array of words used in the string by using str_word_count next you are using array_count_values to get the frequency of the words.

Now to sort the words based on frequency you can use asort for ascending order or arsort for descending order.

$result = array_count_values(
              str_word_count(
                  strip_tags($contents), 1
                  ));
asort($result);  // Add this.

See it

0

精彩评论

暂无评论...
验证码 换一张
取 消