Jump to content


Photo

deals with simple arrays


  • Please log in to reply
2 replies to this topic

#1 phporcaffeine

phporcaffeine
  • Members
  • PipPipPip
  • Advanced Member
  • 361 posts
  • LocationOhio, USA

Posted 31 May 2006 - 02:31 PM


$array = array("12:00", "13:20", "12:00", "15:04", "18:30", "15:04", "19:19", "15:04", "18:30");

1.) The first thing I need to do is identify which elements have duplicate values.

2>.) The second thing I need to do is to determin which of the elements that have dupes, has the "most" duplicates.

so in the above the elements with duplicates are;

12:00
15:04
18:30

the element that has the MOST duplicates is;

15:04

I basically need a way to do that.

Any Ideas?

TIA



Who would have thunk it?

" array_count_values(); " , lol

Thanks anyhow folks
Thanks,

Ryan Huff
President & Founder, MyCodeTree
support@mycodetree.com | http://mycodetree.com

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 31 May 2006 - 08:51 PM

That's the one
$k = array_count_values($array);

then
    // sort in descending order
arsort ($k);
    // get top item
list ($val, $count) = each($k);
echo "$val occurs $count times";

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 mushroom

mushroom
  • Members
  • PipPipPip
  • Advanced Member
  • 41 posts
  • LocationQueen Charlotte B. C. Canada

Posted 01 June 2006 - 06:14 PM

You can use some advanced array functions

$new_array= array_unique($old_array); # get rid of dups
$new_array2= array_diff_assoc($old_array,$new_array); # get only the dups
$keys_old_array= array_keys($new_array2); # keys of dups in old_array other than the first appeance.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users