Count duplicate element of an array in PHP without using in-built function

$arraychars=array("green","red","yellow","green","red","yellow","green");
echo $arrlength=count($arraychars);
$arrCount=array();
for($i=0;$i<$arrlength-1;$i++){
	$key=$arraychars[$i];
	if(@$arrCount[$key]>=1){
		
		$arrCount[$key]++;
	} else{
		$arrCount[$key]=1;
	}
	
	//echo $arraychars[$i];
}
echo "<pre>";
print_r($arrCount);

OUTPUT:

Array
(
    [green] => 3
    [red] => 2
    [yellow] => 2
)

Add a Comment

Your email address will not be published. Required fields are marked *