1

I need to get one time occurence on my array, with my code I get only first result here is my example code:

$arr=array("a","a","b","c","d");
$arrs=array_count_values($arr);
for ($i=0; $i<count($arr); $i++)
{
    if($arrs[$arr[$i]]==1)
    {
        //do something...in this example i expect to receive b c and d
    }
}

Thanks in advance

ciao h

6
  • What do you mean by "get" and "receive"? What do you want to do with the result? Commented Apr 12, 2011 at 12:49
  • I mean same thing excuse me my English is really poor.. Commented Apr 12, 2011 at 12:57
  • "Same thing"? I am asking what you want to do with the result. Commented Apr 12, 2011 at 13:07
  • @Tomalak: I use them to exclude values from filering conditions.. Commented Apr 12, 2011 at 13:17
  • We need more specific information. Can you expand on your code example? What does "do something" mean? Commented Apr 12, 2011 at 13:19

5 Answers 5

2
$arr=array("a","a","b","c","d");
$arrs=array_count_values($arr);
for ($i=0; $i<count($arr); $i++)
{
    if($arrs[$arr[$i]]==1)
    {
        echo $arr[$i];
    }
}

That should display bcd

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

2 Comments

Are you working with different code? The code provided shows bcd like you're asking.
My code is a little more complex but I do not alter contents of '$arr' I simply filter it by 'if' conditions..I think that my '$arr' values are not really clear...
1
$arr=array("a","a","b","c","d");
$result = array();
$doubles = array();
while( !empty( $arr ) ) {
    $value = array_pop( $arr );
    if( !in_array( $value, $arr ) 
            && !in_array( $value, $doubles ) ) {
        $result[] = $value;
    }
    else {
        $doubles[] = $value;
    }
}

4 Comments

Thanks for answering Jochen, your code is really interesting but get others 2 arrays does not help me in this case :(
then just replace $result[] = $value; with echo $value; and delete the else branch ;)
excuse me is it correct in your if condition !in_array($value, $arr) the result is in every case false? It's not better to write !in_array($value, $result) thanks again
the condition !in_array($value, $arr) is true if $value doesn't exists again in the array... in the example above it would be true for the last 4 values from the array
1

May be you've miss your real results:

$arr=array("a","a","b","c","d");
$arrs=array_count_values($arr);
/*
now $arrs is:
array (
  'a' => 2,
  'b' => 1,
  'c' => 1,
  'd' => 1,
)
*/

foreach($arrs as $id => $count){
 if($count==1) {
   // do your code
 }
}

/*******************************************************/
/* usefull version                                     */
/*******************************************************/

$arr=array("a","a","b","c","d");
$arrs=array_count_values($arr);


foreach($arr as $id ){
 if($arrs[$id]==1){ 
   // do your code
   echo "$id is single\n";
 }
}

1 Comment

Thanks for answering Ivan but I need to cycle $arr and during that find the occurence of values inside it so I cannot use your example..
0

You just need to retrieve any value which only occurs once in the array, right? Try this:

$arr=array("a","a","b","c","d");
$arrs=array_count_values($arr);

foreach ($arrs as $uniqueValue => $count)
{
  if($value == 1) {
    echo $uniqueValue; 
  }
}

array_count_values returns an associative array where the key is the value found and its value is the number of times it occurs in the original array. This loop simply iterates over each unique value found in your array (i.e. the keys from array_count_values) and checks if it was only found once (i.e. that key has a value of 1). If it does, it echos out the value. Of course, you probably want to do something a bit more complex with the value, but this works as a placeholder.

Comments

-1
$count = 0;
foreach(array("a","a","b","c","d") as $v){
    if($v == 1){$count++;}
}

1 Comment

Just curious, how does that answer the question...?

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.