1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

array help please

Discussion in 'PHP' started by amorph, May 18, 2007.

  1. #1
    I have a variable which holds some words:

    $words = 'toyota, bmw, audi, opel, audi, bmw, renault';

    How do I get to this result of uniwue words holding the number of occurences, ordered by occurences descending:

    bmw(2)
    audi(2)
    toyota(1)
    opel(1)
    renault(1)

    Thank you for your help.
     
    amorph, May 18, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    
    <?php
    
    $words = 'toyota, bmw, audi, opel, audi, bmw, renault';
    
    $tmp = array_map('trim', explode(',', $words));
    sort($tmp);
    
    foreach (array_count_values($tmp) AS $word => $count)
    {
    	echo "{$word} ({$count})<br />\n";
    }
    
    ?>
    
    PHP:
     
    nico_swd, May 18, 2007 IP
  3. amorph

    amorph Peon

    Messages:
    200
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    wow..that did the trick. Is there any way of ordering them by occurences descending? Thank you very much.
     
    amorph, May 18, 2007 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    Yeah I edited my post at the same time you replied. I should read better before I reply. :p
     
    nico_swd, May 18, 2007 IP
  5. amorph

    amorph Peon

    Messages:
    200
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thank you so much nico. That helped.
     
    amorph, May 18, 2007 IP
  6. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #6
    beat me too it

    
    <?php
    $words = 'toyota, bmw, audi, opel, audi, bmw, renault, bmw, audi, audi, audi,audi';
    
    function count_list_to_array( $words )
    {	
     	$list = array_count_values( split(", ?", $words ) ) ;
    	return $list;
    }
    $list = count_list_to_array( $words ) ;
    arsort( $list );
    foreach( $list as $index => $occured )
    {
    	printf("%s ( %s )<br/>\n", $index, $occured );
    }
    
    PHP:
     
    krakjoe, May 18, 2007 IP
  7. amorph

    amorph Peon

    Messages:
    200
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Ok. Thanks a lot guys. Here's my application so far which works very well except for a small bug.

    I have a table "nominated" which, besides some other data such as title-subitle etc, holds some tags separated by commas for each record in part. Example of record:

    title = toyota MR2
    subtitle - damn fast car
    tags = fast, toyota, mr2, car

    The code that I use to put all the tags together and also build the cloud looks like this:

    mysql_select_db($database_anunturi);
    	$query = "SELECT tags FROM nominated ORDER BY ID DESC";
    	$do_query = mysql_query($query) or die(mysql_error());
    	$row = mysql_fetch_assoc($do_query);
    	$totalRows = mysql_num_rows($do_query);
    	
    	if($totalRows>0)
    	{
    		$array = '';
    		do {
    			$array .= $row['tags'] . ' ';
    		} while($row = mysql_fetch_assoc($do_query));
    	}
    
    	require('stopwords.php');
    	
    	$stop = new Stopwords();	
    	
    	$array = $stop->parseString($array);
    	
    	$split = split(' ', $array);
    	
    	
    	$words = '';
    	foreach($split as $key):
    	
    		$words .= $key . ',';
    		
    	endforeach;
    
    	function count_list_to_array( $words )
    	{   
    	    $list = array_count_values( split(", ?", $words ) ) ;
    	    return $list;
    	}
    	$list = count_list_to_array( $words ) ;
    	arsort( $list );
    	foreach( $list as $index => $occured )
    	{
    	    printf("%s (%s)<br/>\n", $index, $occured );
    	}
    PHP:
    Everything works fine as I said except for 163 blank spaces that are counted.
    require('stopwords.php'); is for taking down viagras, punctuation and other characters (cleaning the content).

    The result is something like:
    (163)
    toyota(3)
    mr2(2)
    red(2)
    car(1)


    Any idea how to get rod of that white space?
     
    amorph, May 18, 2007 IP
  8. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #8
    
    $words = 'toyota, bmw, audi, opel, audi, bmw, renault,,,,,';
    
    $tmp = preg_split('/,\s*/', $words, -1, PREG_SPLIT_NO_EMPTY);
    asort($tmp);
    
    foreach (array_count_values($tmp) AS $word => $count)
    {
    	echo "{$word} ({$count})<br />\n";
    }
    
    
    PHP:
     
    nico_swd, May 18, 2007 IP
    coderbari likes this.
  9. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #9
    mysql_select_db($database_anunturi);
        $query = "SELECT tags FROM nominated ORDER BY ID DESC";
        $do_query = mysql_query($query) or die(mysql_error());
        $row = mysql_fetch_assoc($do_query);
        $totalRows = mysql_num_rows($do_query);
        
        if($totalRows>0)
        {
            $array = '';
            do {
                $array .= $row['tags'] . ' ';
            } while($row = mysql_fetch_assoc($do_query));
        }
    
        require('stopwords.php');
        
        $stop = new Stopwords();    
        
        $array = $stop->parseString($array);
        
        $split = split(' ', $array);
        
        
        $words = '';
        foreach($split as $key):
        
            $words .= $key . ',';
            
        endforeach;
    
        function count_list_to_array( $words )
        {  
            $list = array_count_values( array_values( split(", ?", $words ) ) ) ;
            return $list;
        }
        $list = count_list_to_array( $words ) ;
        arsort( $list );
        foreach( $list as $index => $occured )
        {
            if( $index )
            {
                printf("%s (%s)<br/>\n", $index, $occured );
            }
        }
    PHP:
     
    krakjoe, May 18, 2007 IP
  10. amorph

    amorph Peon

    Messages:
    200
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Wow....digitapoint is the best. I ran out of net due to a storm here and never got the chance to thank you. I must admit that I asked on many forums and only here someone really helped. Thank you so much. It works. IT WORKS. IT's ALLLLIIIIVEEE!!! :))
     
    amorph, May 19, 2007 IP
  11. coderbari

    coderbari Well-Known Member

    Messages:
    3,168
    Likes Received:
    193
    Best Answers:
    0
    Trophy Points:
    135
    #11
    i am a fan of krakjoe.his codes are so interesting.also nico_swd,he is so infomative.i recently joined this forum and here i saw member's give solutions of problems.i have never seen such an excellent forums.i learnt many things here and i am still learning.
     
    coderbari, May 19, 2007 IP