Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

PHP help needed

  • 07-02-2009 7:53pm
    #1
    Closed Accounts Posts: 94 ✭✭


    Hi,

    I have a problem and I don't know how to even approach it.

    I have an array that goes like this:

    Adam
    Peter
    Adam
    Peter
    Peter
    Micheal

    What I want PHP to do is total the number of duplicates each name has and place them in order, the ones with the most duplicates first.

    So the end array would be:

    Peter
    Adam
    Micheal

    There were 3 Peter's in the original array, so he goes first, 2 Adam's so he's second an one micheal, so he's last.

    How would I go about doing this?

    Thanks!


Comments

  • Closed Accounts Posts: 94 ✭✭gnomer


    So nobody can even point me in the right direction?


  • Closed Accounts Posts: 18,163 ✭✭✭✭Liam Byrne


    $arr=array("Adam","Peter","Adam", etc.....
    $removedDuplicates=array_unique($arr);

    $number_of_duplicates=count($arr)-count($removedDuplicates);


  • Closed Accounts Posts: 94 ✭✭gnomer


    Liam Byrne wrote: »
    $arr=array("Adam","Peter","Adam", etc.....
    $removedDuplicates=array_unique($arr);

    $number_of_duplicates=count($arr)-count($removedDuplicates);
    Yes, I know that, but it still doesn't help my problem, But many thanks for trying.


  • Closed Accounts Posts: 18,163 ✭✭✭✭Liam Byrne




  • Closed Accounts Posts: 94 ✭✭gnomer


    Liam Byrne wrote: »

    Many thanks for that, that helped me a lot. But even if I know how many occurances there are of a certain name how am I going to organise the ones with the most first in a new array?

    I would need to know which name has the smallest duplicates and which has 2nd smallest etc.

    Thats my main problem.


  • Advertisement
  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    gnomer wrote: »
    Many thanks for that, that helped me a lot. But even if I know how many occurances there are of a certain name how am I going to organise the ones with the most first in a new array?

    I would need to know which name has the smallest duplicates and which has 2nd smallest etc.

    Thats my main problem.
    http://ie2.php.net/count


  • Closed Accounts Posts: 18,163 ✭✭✭✭Liam Byrne


    gnomer wrote: »
    Many thanks for that, that helped me a lot. But even if I know how many occurances there are of a certain name how am I going to organise the ones with the most first in a new array?

    I would need to know which name has the smallest duplicates and which has 2nd smallest etc.

    Thats my main problem.

    Put the results into an array and sort the resulting array....

    function findDuplicates($data,$dupval) {
    $nb= 0;
    foreach($data as $key => $val)
    if ($val==$dupval) $nb++;
    return $nb;
    }

    $arr=array("Adam","Peter","Adam", etc
    $arr2=array();

    foreach ($arr as $k=>$v) {
    if (!(isset($arr2[$k]))) $arr2[$k]=0;
    $arr2[$k]=findDuplicates($arr,$v);
    }

    sort($arr2);


  • Closed Accounts Posts: 94 ✭✭gnomer


    I'm not sure if its something I did, but can anyone check this code? its still not working correctly.

    NOTE: its already connected to mysql database, if I echo $arr it is correct
    [PHP]
    <?php

    function findDuplicates($data,$dupval) { //this function finds duplicates
    $nb= 0;
    foreach($data as $key => $val)
    if ($val==$dupval) $nb++;
    return $nb;
    }


    $result = mysql_query("SELECT `name` FROM `blah`"); //get the names
    $arr=array(); // create $arr


    while($row2=mysql_fetch_assoc($result)) //put them in an array, $arr
    {
    $arr[]=$row2;
    }
    print_r($arr);

    $arr2=array(); //create $arr2

    foreach ($arr as $k=>$v) { // fix them all out
    if (!(isset($arr2[$k]))) $arr2[$k]=0;
    $arr2[$k]=findDuplicates($arr,$v);
    }

    sort($arr2); // sort them


    print_r($arr2); //print it

    ?>[/PHP]


  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    Why don't you use SQL to do the sorting automatically for you? or must you do with in PHP.


  • Closed Accounts Posts: 94 ✭✭gnomer


    Webmonkey wrote: »
    Why don't you use SQL to do the sorting automatically for you? or must you do with in PHP.

    I'm not sure exactly how I would do it in the SQL query, It doesnt have to be in PHP.

    But thats a good idea tho, if anyone can help me do it either way I would appreciate it.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    SELECT name, count(*) as number FROM `blah` group by name order by number DESC
    

    Then you will get back 3 rows, 2 columns 'name' and 'number' - but you can ignore the number column.


  • Closed Accounts Posts: 94 ✭✭gnomer


    Many thanks! I have it working now!


  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    Good stuff


Advertisement