Jump to content

Archived

This topic is now archived and is closed to further replies.

phporcaffeine

deals with simple arrays

Recommended Posts


$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

Share this post


Link to post
Share on other sites
That's the one
[code]$k = array_count_values($array);[/code]

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.