Return to Snippet

Revision: 31430
at September 4, 2010 21:07 by beneberle


Updated Code
<?php 

$array = array(1,1,1,2,2,2,2,3);

function returnValueOnce($array){

     $arrayTrim = array_keys(array_count_values($array));
     //array_count_values returns an array( [1]=>3 [2]=> 4 [3]=> 1 ) 

     return $arrayTrim;
     //returns numerical array(1,2,3)
}
// the array_unique() function does this too, so now I'm the wiser....

// it removes duplicate values from an array. If two or more array values are the same, the // first appearance will be kept and the other will be removed.
?>

Revision: 31429
at September 4, 2010 20:59 by beneberle


Updated Code
<?php 

$array = array(1,1,1,2,2,2,2,3);

function returnValueOnce($array){

     $arrayTrim = array_keys(array_count_values($array));
     //array_count_values returns an array( [1]=>3 [2]=> 4 [3]=> 1 ) 

     return $arrayTrim;
     //returns numerical array(1,2,3)
}

?>

Revision: 31428
at September 4, 2010 20:53 by beneberle


Updated Code
<?php 

$array = array(1,1,1,2,2,2,2,3);

function returnValueOnce($array){

     $arrayTrim = array_keys(array_count_values($array);
     //array_count_values returns an array( [1]=>3 [2]=> 4 [3]=> 1 ) 

     return $arrayTrim;
     //returns numerical array(1,2,3)

?>

Revision: 31427
at September 4, 2010 20:42 by beneberle


Initial Code
<?php 

$array = array(7,7,7,9,9,7,7,9,9,9,9,9,9,7,7);

$arrayCount = array_count_values($array);
// $arrayCount becomes a key = > value array equal to value = > # of occurrences
 
$arrayTrim = array_keys($arrayCount);
//returns only the values

print_r($arrayTrim);

?>

Initial URL
http://www.w3schools.com/php/func_array_unique.asp

Initial Description
I needed to trim a simple numerical array that might have duplicate values into an array with no duplicates. I came up with this before finding array_unique().   D\'oh.

Initial Title
Count occurrences of value in array, return values once

Initial Tags
php

Initial Language
PHP