Re: [PHP] Counting elements in an array
$i = 0; foreach ($fruit as $k) if ('prange' == $k) $i++; (The result is $i, of course) Jeff McKeon wrote: How would I count the number of elements in an array that had a certain value? In other words, if I have $fruit = array(orange,orange,apple,bananna,orange,apple,pear); How could I get the number of elements from $fruit where the value is orange? Thanks, Jeff -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Counting elements in an array
[snip] How would I count the number of elements in an array that had a certain value? In other words, if I have $fruit = array(orange,orange,apple,bananna,orange,apple,pear); How could I get the number of elements from $fruit where the value is orange? [/snip] http://www.php.net/array_keys -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Counting elements in an array
On Wednesday 19 November 2003 23:24, Jeff McKeon wrote: How would I count the number of elements in an array that had a certain value? In other words, if I have $fruit = array(orange,orange,apple,bananna,orange,apple,pear); How could I get the number of elements from $fruit where the value is orange? array_count_values() -- Jason Wong - Gremlins Associates - www.gremlins.biz Open Source Software Systems Integrators * Web Design Hosting * Internet Intranet Applications Development * -- Search the list archives before you post http://marc.theaimsgroup.com/?l=php-general -- /* They also surf who only stand on waves. */ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Counting elements in an array
Jason Wong wrote: On Wednesday 19 November 2003 23:24, Jeff McKeon wrote: How would I count the number of elements in an array that had a certain value? In other words, if I have $fruit = array(orange,orange,apple,bananna,orange,apple,pear); How could I get the number of elements from $fruit where the value is orange? array_count_values() That gives me: Warning: array_count_values(): Can only count STRING and INTEGER values! The values of the array elements should be integer. Jeff -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Counting elements in an array
On Thursday 20 November 2003 00:33, Jeff McKeon wrote: Jason Wong wrote: On Wednesday 19 November 2003 23:24, Jeff McKeon wrote: How would I count the number of elements in an array that had a certain value? In other words, if I have $fruit = array(orange,orange,apple,bananna,orange,apple,pear); How could I get the number of elements from $fruit where the value is orange? array_count_values() That gives me: Warning: array_count_values(): Can only count STRING and INTEGER values! Well, so change your array definition so that it holds an array of strings rather than constants. -- Jason Wong - Gremlins Associates - www.gremlins.biz Open Source Software Systems Integrators * Web Design Hosting * Internet Intranet Applications Development * -- Search the list archives before you post http://marc.theaimsgroup.com/?l=php-general -- /* Hello. I know the divorce rate among unmarried Catholic Alaskan females!! */ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Counting elements in an array
[snip] $fruit = array(orange,orange,apple,bananna,orange,apple,pear); How could I get the number of elements from $fruit where the value is orange? [/snip] From http://www.php.net/array_keys with additions; $array = array(blue, red, green, blue, blue); $countArray = count((array_keys($array, blue))); echo $countArray . \n; 3 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Counting elements in an array
Jeff McKeon wrote: How would I count the number of elements in an array that had a certain value? In other words, if I have $fruit = array(orange,orange,apple,bananna,orange,apple,pear); How could I get the number of elements from $fruit where the value is orange? Thanks, Jeff $temp = array_keys ( $fruit, orange ); echo ( sizeof ( $temp ) ); -- By-Tor.com It's all about the Rush http://www.by-tor.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php