I'm trying to get the most popular and unique value from each array. I already have the means of tallying points for these objects in the arrays. Thinking these first 3 arrays as 'categories':
 (obviously example items)

```
$item1 = array( spoon => 5, knife => 3, fork => 2); 
$item2 = array( fork => 5, knife => 4, spoon => 2);
$item3 = array( spoon => 4, knife => 3, fork => 1);

```

That was the tally for each individual category. I then make a key\_array for each category and another array consisting of the most popular from each.

```
$max1 = array_keys($item1);
$max2 = array_keys($item2);
$max3 = array_keys($item3);

$maxset = array( 0 => $max1[0], 1 => $max2[0], 2 => $max3[0]);

```

With the example, I have 2 spoons in `$maxset`. So now, I need to check for duplicates and choose the more popular one:

```
    foreach($maxset as $key => $value){
        $check = array();                                 
        if(in_array($value, $check)){
            $checkkey = array_search($value, $check);     //get key where $value already exists
            $checkval = ${'item' . $checkkey+1}[$value];  // get popularity of original copy
            $checkvalnew = ${'item' . $key+1}[$value];    // get popularity of duplicate

            if ($checkval > $checkvalnew){                // if the original is more popular
                $newvalue = ?;                    // ! 
                array_push($check, $newvalue);      
            }                   
            else{                                         // if the duplicate is more popular
                $check[$checkkey] = ?;            // !
                array_push($check, $value);
            }
        }else{
            array_push($check, $value);
        }
    }

```

Where it has a `'// !'`: I'm not sure what to do in here. If I get a new value, then what if it too is a duplicate? I'd need a function to check it, but then how would I not get stuck in a loop?

In the end, it should look like:

```
$check = array(0 => spoon, 1 => fork, 2 => knife); // it would be inserted into a database

```

Am I using the wrong method to achieve this? Is there, perhaps, a much more efficient language to do this task? Thanks for the help!

**Edit:** (Further example sets to determine formula) \*\* where answer should be selected

```
$item1 = array(red => 5, **green** => 4, yellow => 2, blue => 1);
$item2 = array(**blue** => 5, green => 3, red => 2, yelow => 1);
$item3 = array(**red** => 6, blue => 4, yellow => 2, green => 1);

$check = array(0 => green, 1 => blue, 2 => red)  // red1 got replaced by more popular red3
//Total point: 15

$item1 = array(yellow => 5, blue => 4, **red** => 2, green => 1);
$item2 = array(**blue** => 5, green => 3, red => 2, yellow => 1);
$item3 = array(**yellow** => 6, green => 4, red => 2, blue => 1);

$check = array(0 => red, 1 => blue, 2 => yellow)  // yellow1 got replaced by more popular yellow3. The replacement for yellow1 was blue1, which lost to blue2. 
//Total point: 13

```