Inserting only unique values into an array

11,477

Solution 1

As you loop thru your values you can do the following:

isset( $valsArray[$val] ) ? $valsArray[$val]++ : $valsArray[$val]=1;

example:

$valsArray=array();

$val="foo";
isset($valsArray[$val])?$valsArray[$val]++:$valsArray[$val]=1;
$val="foo";
isset($valsArray[$val])?$valsArray[$val]++:$valsArray[$val]=1;
$val="bar";
isset($valsArray[$val])?$valsArray[$val]++:$valsArray[$val]=1;

print_r($valsArray);

will get you:

Array ( [foo] => 2 [bar] => 1 ) 

Solution 2

$valsArray = array_unique($valsArray);

Solution 3

@$valsArray[$val]++;

should do it for you. New entries get added as a key with a value of 1, existing entries get their value incremented. The @ avoids an E_NOTICE being thrown every time this encounters a new value.

Share:
11,477
karl
Author by

karl

Updated on June 14, 2022

Comments

  • karl
    karl about 2 years

    I have a set of values that I'm pushing into an array in the order they occur

    $valsArray = array(); 
    
    //I process each value from a file (code removed for simplicity) 
    //and then add into the array 
    $valsArray[] = $val; 
    

    How do I turn this into an associative array instead where the value gets inserted (as $key of associative array) only if it doesn't exist. If it does exist increment its count ($value of associative array) by 1. I'm trying to find a more efficient way of handling those values compared to what I'm doing now.

  • goat
    goat over 14 years
    Will generate E_NOTICE level errors when the element doesn't yet exist.
  • karl
    karl over 14 years
    @chris Unfortunately that's true, although it's a very good answer and came first before zaf's. I think for future readers with the same question, I'll choose zaf's answer since it's more complete.
  • Sparr
    Sparr over 14 years
    Ahh, I didn't realize it generated an error. Drat. Thanks for pointing that out. I would edit to something like zaf's but he already did that nicely so I'll leave mine as an example of the shorter less-nice way to do it.
  • Sparr
    Sparr over 14 years
    Added the @ to make it cleaner :)
  • karl
    karl over 14 years
    @Sparr, I'm curious now, which is better practice? Does the @ just suppress the warning (i.e. it still gets triggered, just doesn't show)?
  • Sparr
    Sparr about 14 years
    I actually used this in production code on the same day I posted it. The @ suppresses the warning. I prefer my way, since it's one line of code with one or two lines of comments, which imho makes it a lot easier to read for whoever has to maintain it later.
  • goat
    goat almost 12 years
    @tombom, i disagree. The link is to the authorative php documentation, which includes a great description + usage example. Spending time duplicating a durable resource is not a good use of time. If I was linking to some random no name blog....then I might agree. but in general, hyperlinks to focused info are quite awesome, and very appropriate as answers here.