Inserting only unique values into an array

Posted by karl on Stack Overflow See other posts from Stack Overflow or by karl
Published on 2010-03-26T15:47:36Z Indexed on 2010/03/26 15:53 UTC
Read the original article Hit count: 338

Filed under:
|
|

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.

© Stack Overflow or respective owner

Related posts about php

Related posts about arrays