0

After both myself and a friend searching for hours upon end, and trying numerous things we are unable to find out how to echo only the duplicate values of array_count_values result. I will break it down:

We have numerous select boxes, which build the array when sent via GET, for example:

    <div class="form-group col-sm-2 mb-sm">
            <select name="select[]" class="form-control">
                <option value="" disabled="" selected="">Select</option>
                <option value="optionOne">Option 1</option>
                <option value="optionTwo">Option 2</option>
            </select>
        </div>
        <div class="form-group col-sm-2 mb-sm">
            <select name="select[]" class="form-control">
                <option value="" disabled="" selected="">Select</option>
                <option value="optionOne">Option 1</option>
                <option value="optionTwo">Option 2</option>
            </select>
        </div>
        <div class="form-group col-sm-2 mb-sm">
            <select name="select[]" class="form-control">
                <option value="" disabled="" selected="">Select</option>
                <option value="optionOne">Option 1</option>
                <option value="optionTwo">Option 2</option>
            </select>
        </div>

We are then doing the following:

if (max(array_count_values($_GET['select'])) == 2) {
   $twoSelected = '2 of the selections are the same, which were (DUPLICATE SELECTION HERE)';
}

We have tried a foreach loop, but can't seem to get it to work.

Any help will be very much appreciated.

Kind Regards

5
  • can post your get parameter $_GET['select'] array value here Commented Jun 22, 2017 at 10:22
  • All you need to do is loop over the result of array_count_values, and output all the keys that have a value > 1. Commented Jun 22, 2017 at 10:29
  • @JYoThI I have edited the post Commented Jun 22, 2017 at 10:31
  • please post var_dump($_GET['select']); @DisplayName Commented Jun 22, 2017 at 10:34
  • @JYoThI Thank you but I have accepted an answer, I would've thought you could tell what the var_dump would output from the select boxes that I added in to the post, sorry. Commented Jun 22, 2017 at 10:37

2 Answers 2

2

Try this one

$array = array("test", "hello", "test", "world", "hello");

$duplicatedValuesArray = array_keys(array_filter(array_count_values($array), function($v) {
    return $v > 1;
}));

echo implode(', ',$duplicatedValuesArray);
Sign up to request clarification or add additional context in comments.

1 Comment

This is perfect thank you. I would up-vote it but someone down-voted my post for some reason without even mentioning why so I now have less than 15 rep...
0

I think below code is helpful for you to get duplicate value.

$array = array(1=>'12334',2=>'123345',3 =>'Helloo' ,4=>'hello', 5=>'helloo');

// Convert every array value to uppercase, and remove all duplicate values
$notdublicates = array_unique(array_map("strtoupper", $array));

// The difference in the original array $array, and the $notdublicates array
// will be the duplicate values
$getduplicates = array_diff($array, $notdublicates);
print_r($getduplicates);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.