up vote 1 down vote favorite
share [fb]

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

link|improve this question

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

5 Answers

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";
 }
}
link|improve this answer
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.. – haltman Apr 12 at 12:28
feedback

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.

link|improve this answer
feedback
$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;
    }
}
link|improve this answer
Thanks for answering Jochen, your code is really interesting but get others 2 arrays does not help me in this case :( – haltman Apr 12 at 12:35
then just replace $result[] = $value; with echo $value; and delete the else branch ;) – Jochen Hilgers Apr 12 at 13:23
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 – haltman Apr 12 at 13:45
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 – Jochen Hilgers Apr 12 at 15:00
feedback
$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

link|improve this answer
Thanks for answering Aaron but how I've written that does not work correctly... – haltman Apr 12 at 12:24
Are you working with different code? The code provided shows bcd like you're asking. – Aaron W. Apr 12 at 12:27
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... – haltman Apr 12 at 12:42
feedback
$count = 0;
foreach(array("a","a","b","c","d") as $v){
    if($v == 1){$count++;}
}
link|improve this answer
Just curious, how does that answer the question...? – MeLight Apr 12 at 12:20
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.