Hi,
I want to Compare & count elements in array??

suppose in my array
$arr={140,123,34,140,23,123,140,140,123}

I want to display like
140 - 4 ( comes in array 4 times)
123 - 3
34 - 1
23 -1

How to do this??

sort your array as decending and

apply foreach loop and check each value if the every new value is equal to last value $count++ else move to next.

What about the array_count_values() function? I have never used it, but I seems to be what you are looking for.

Does anyone on this forum check php.net at all?

Here is the hard way:

$array = array( 140,123,34,140,23,123,140,140,123 );
$data = array();

foreach( $array as $val ) {
	if ( array_key_exists( $val,$data ) ) {
		$data[$val]++;
	}
	else {
		$data[$val] = 1;
	}
}

print_r( $data );
commented: Most posters are too lazy to RTFM. +21

yess its a very good option

<?
$arr=array(140,123,34,140,23,123,140,140,123);
print_r(array_count_values($arr));
?>

array_count_values() function works...
thank you guys for u kind help..

:) thats great. marked your issue as solved plz

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.