hi

I've been looking at:
http://www.phpbuilder.com/board/showthread.php?threadid=10279873&highlight=counting+same+array+values

which seems to be the problem I have, but the solution doesn't work for me!

Basically, I have an array, and it may contain duplicate values. So I want to search through, and count how many duplicates there are for each value, and then delete the duplicates.

I've been trying array_count_values() but when I print_r, the values don't group!!!!!

I've seen array_unique which seems like it will remove the values once they've been counted, but can someone help me piece together this puzzle!

Cheers

    array_count_values() should work. What you mean "the values don't group"? Give us a bit of code that illustrates what you're doing.

      Hi

      a print_r of my array gives this (so you can see what my array is)

      Array
      (
          [0] => 
          [1] => 0 loc/secure/viewlogs.php .
      
      [2] => 1 loc/secure/editgb.php .
      
      [3] => 2 loc/secure/editsb.php .
      
      [4] => 3 loc/secure/contactdata.php .
      
      [5] => 4 loc/secure/archiveworklog.php .
      
      [6] => 5 loc/secure/viewlogs.php .
      
      [7] => 6 loc/secure/viewlogs.php .
      
      [8] => 7 loc/secure/viewlogs.php .
      
      [9] => 8 loc/secure/viewlogs.php .
      
      [10] => 9 loc/secure/viewlogs.php .
      
      [11] => 10 loc/projects/viewlogs.php .
      
      [12] => 11 loc/projects/index.php .
      )
      

      and this is the code I'm using to display the grouping (hopefully!)

      print_r (array_count_values($aHolder));
      

      It would be great if once this was working, I could use a foreach loop to get each invdivual result, but I think I'm getting ahead of myself here!

      Thanks for help light you can shed on this!

        if thats you $aHolder array then each value is unique so any count wouldnt work

        why are there numbers in front of the paths anyway?

          Hi

          My fault about the numbers. Earlier on in the code, before I added the items to the array I added a number from the for loop. I got rid of that and I now have the results I should have now!

          Great! Here they are:

          Array
          (
              [] => 1
              [loc/secure/viewlogs.php .
          ] => 6
              [loc/secure/editgb.php .
          ] => 1
              [loc/secure/editsb.php .
          ] => 1
              [loc/secure/contactdata.php .
          ] => 1
              [loc/secure/archiveworklog.php .
          ] => 1
              [loc/projects/viewlogs.php .
          ] => 1
              [loc/projects/index.php .] => 1
          )
          

          However, sorry for asking a really newbie type question, but how can I use this data!?!?!? I'm using print_r at the moment, but I'd like to output it in form:
          item1 6
          item2 1
          item3 1

          etc, where 'item' are the paths from the array.

          If you could point me in the right direction that'd be really helpful thanks!

            Summat like

            echo "<ul>";
            foreach($counts as $path=>$count)
                echo "<li>$path: $count</li>";
            echo "</ul>";
            

              thanks

              really grateful for the help and i'm now a lot further on that I was early on!!! cheers!

              My output I now have it just about as I wanted it:

              loc/secure/viewlogs.php .
              : 6
              loc/secure/editgb.php .
              : 1
              loc/secure/editsb.php .
              : 1
              loc/secure/contactdata.php .
              : 1
              loc/secure/archiveworklog.php .
              : 1
              loc/projects/viewlogs.php .
              : 1
              loc/projects/index.php .: 1
              

              Another question if I may. It is automatically putting a line break in after the entry, before the number. Do you know why this is happening?
              My initial feeling is that it's a problem with the data being put into the array (and the item in the array contains a line break) but I could be wrong.
              I've tried to str_replace any linebreaks, but no to avail!

              Also odd the last line should work fine!

              Any ideas? and thanks again for the help you guys have given me.

              EDIT: After trimming the array item the line break disappeared!

                Write a Reply...