0

i have following array values, here spend is repeated,

$array = array('Spend','Spend','Form Conversions','Phone Conversions')

i can detect multiple entries like this

print_r(array_count_values($array));

output is

Array
(
   [Spend] => 2
   [Form Conversions] => 1
   [Phone Conversions] => 1
)

how can i put separate condition to print as follows

if(any key(here spend) found with count = 2 )
{
echo $duplicate element; //edits
}
else
{
 echo $no_duplicate_elements //edits
echo "count = 1 (here Form Conversions,Phone Conversions)";
}
1
  • you need to loop over key i guess. Commented Nov 23, 2017 at 11:26

2 Answers 2

3

how about

foreach(array_count_values($array) as $value => $count){
    if($count > 1){
        echo "hi " . $value . "*";
    }else{
       echo "count = 1 " . $value . "*";
    }
}

?

Sign up to request clarification or add additional context in comments.

1 Comment

I don't see a change in the question - but $value is the key, so the output will be: hi Spend*count = 1 Form Conversions*count = 1 Phone Conversions
2

use function in_array

if (in_array(2, array_count_values($array))) {
    echo "there is at least one word with count = 2";
}

if you want to know what elements are duplicated, then you have to use foreach - check Jameson the dog's answer.

2 Comments

Wow, haven't thought of this. Awsome!
how to display the duplicate element..i have edited question please look in if condition

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.