Firstly thanks in advance :)
I write a code like this way:
$data = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,5];
$result = max(array_count_values($data));
if($result > 1) {
echo 'Duplicate items were found!';
}else{
echo 'No Duplicate';
}
This code works perfectly but when I get data from the server-side like :
$getValue = '1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,5';
$data = [$getValue];
$result = max(array_count_values($data));
if($result > 1) {
echo 'Duplicate items were found!';
}else{
echo 'No Duplicate';
}
code not working, anyone please help me here to sort out.
CodePudding user response:
Try this,
$getValue = '1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,5';
$data = [$getValue];
$data = explode(',', $data[0]);
$result = max(array_count_values($data));
if($result > 1) {
echo 'Duplicate items were found!';
}else{
echo 'No Duplicate';
}
CodePudding user response:
The variable $getValue
is a string, and using $data = [$getValue];
will create an array with that single entry and using max will return 1 yielding "No Duplicate"
What you could do is check if the variable $getValue
is a string, use explode and remove the empty entries.
For this you could use for example array_filter and you can see the documentation what this will remove when using it without a callback.
$getValue = '1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,5';
if (is_string($getValue)) {
$result = max(array_count_values(array_filter(explode(',', $getValue))));
}
if ($result > 1) {
echo 'Duplicate items were found!';
} else {
echo 'No Duplicate';
}
Output
Duplicate items were found!
See a PHP demo.