Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 7 of 7
  1. #1
    Senior Member
    Join Date
    May 2003
    Location
    UK
    Posts
    2,354
    Member #
    1326
    Hi, I have a string of text.

    I explode it using a space as delimeter.

    I need to find out what the most common value in the exploded array.

    I had a look through the functions on php.net but could not see anything.

    One way I thought of is to loop for the length of the array and use a associative array using the word at position $i as the key.
    That way I could get the most common value, but then I would not know what word is the most common..

    Any ideas?

  2.  

  3. #2
    Senior Member Fallout's Avatar
    Join Date
    Aug 2003
    Location
    Richmond, Virginia
    Posts
    543
    Member #
    2748
    You can make an array with array_unique() that takes out any duplicates, and then compare that to the original with array_diff(). Then I guess do some kind of counting on the difference array to see which value is the most common.

    This way is very round-about and I'm sure theres a better way, but I don't know it.

  4. #3
    Senior Member Stylise's Avatar
    Join Date
    Jul 2005
    Location
    Mount Martha, Australia
    Posts
    229
    Member #
    10679
    If you make an associative array with the the word (a string) as the key, you could just increment the value of the key for each exploded value. Then just grab the key for the highest value.

  5. #4
    Senior Member Brak's Avatar
    Join Date
    Apr 2003
    Location
    San Francisco, CA
    Posts
    3,413
    Member #
    1217
    Liked
    2 times
    Check out array_count_values: http://us2.php.net/manual/en/functio...unt-values.php

    I think something like this should work:

    PHP Code:
    $count array_count_values($exploded_array);
    echo 
    key($count[0]); 
    Kyle Neath: Rockstar extraordinare
    The blog | The poetry site | The Spore site

  6. #5
    Senior Member Stylise's Avatar
    Join Date
    Jul 2005
    Location
    Mount Martha, Australia
    Posts
    229
    Member #
    10679
    Something like this would work

    PHP Code:
    function getMostCommonWord($input)
    {
        if (empty(
    $input))
            return -
    1;
            
        
    $words explode(" "strtolower($input));
        
        
    $commonWords[] = "";
        
        for (
    $counter 0$counter sizeOf($words); $counter++)
        {
            if (
    array_key_exists($words[$counter], $commonWords))
                
    $commonWords["$words[$counter]"] = $commonWords[$words[$counter]] + 1;
            else
                
    $commonWords["$words[$counter]"] = 1;
        }

        
    asort($commonWords); // sort the array on value from lowest to highest

        // go to the highest value
        
    if (end($commonWords) == 1)
            return -
    2;
        else
            return 
    key($commonWords); // return the key of the value
    }

    $input "One two Three Four";

    $result getMostCommonWord($input);

    if (
    $result == -1)
        echo 
    "No input was entered";
    elseif (
    $result == -2)
        echo 
    $input " has no common word";
    else
        echo 
    "Most common word is <strong>" $result "</strong>"
    Only downfall to this is if there are more than one "most common" word.

  7. #6
    Senior Member tekp's Avatar
    Join Date
    Jan 2004
    Location
    A small village near a small city near a small cit
    Posts
    918
    Member #
    4667
    Liked
    1 times
    Surely there's a mode() function?

    There should be if there isn't...
    tekp :cheeky: tekponline.com

  8. #7
    Senior Member
    Join Date
    May 2003
    Location
    UK
    Posts
    2,354
    Member #
    1326
    I havent tried this code yet, but I think something like this:
    PHP Code:
    <?php
    $string 
    "I am going to explode -  explode big!";
    $exploded_string explode(" "$string);

    $size sizeof($exploded_string);
    $size $size 1;

    for(
    $i 0$i $size$i++)
       {
           
    $words[$exploded_string[$i]] = $words[$exploded_string[$i]] + 1;
       }

    // sort desc-asc
    //return the value position at index 0 of sorted array

    ?>
    Something like that..


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 11:17 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com