note 95713 modified in function.array-count-values by danbrown

From: Date: Mon, 18 Jan 2010 15:09:51 +0000
Subject: note 95713 modified in function.array-count-values by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-165079@lists.php.net to get a copy of this message
Here is a Version with one or more arrays, which have similar values in it: Use $lower=true/false to ignore/set case Sensitiv. <?php $ar1[] = array("red","green","yellow","blue"); $ar1[] = array("green","yellow","brown","red","white","yellow"); $ar1[] = array("red","green","brown","blue","black","yellow"); #$ar1= array("red","green","brown","blue","black","red","green"); // Possible with one or multiple Array $res = array_icount_values ($ar1); print_r($res); function array_icount_values($arr,$lower=true) { $arr2=array(); if(!is_array($arr['0'])){$arr=array($arr);} foreach($arr as $k=> $v){ foreach($v as $v2){ if($lower==true) {$v2=strtolower($v2);} if(!isset($arr2[$v2])){ $arr2[$v2]=1; }else{ $arr2[$v2]++; } } } return $arr2; } /* Will print: Array ( [red] => 3 [green] => 3 [yellow] => 4 [blue] => 2 [brown] => 2 [white] => 1 [black] => 1 ) */ ?> --was-- Here is a Version with one or more arrays, which have similar values in it: Use $lower=true/false to ignore/set case Sensitiv. <?php $ar1[] = array("red","green","yellow","blue"); $ar1[] = array("green","yellow","brown","red","white","yellow"); $ar1[] = array("red","green","brown","blue","black","yellow"); $res = array_icount_values ($ar1); print_R($res); function array_icount_values($arr,$lower=true) { $arr2=array(); foreach($arr as $k=> $v){ foreach($v as $v2){ if($lower==true) {$v2=strtolower($v2);} if(!isset($arr2[$v2])){ echo " ". $arr2[$v2]=1; }else{ echo " ". $arr2[$v2]++; } } } return $arr2; } ?> http://php.net/manual/en/function.array-count-values.php

« previous php.notes (#165079) next »