vincej Posted May 8, 2012 Share Posted May 8, 2012 HI - I have tried COUNT and array_count _values but they don't do what I want. I have an array of 'quantities' relating to orders. I want to count how many key=>value pairs have a positive value that is to say Not 0 or null or "". So for example array ( shoes=>5, coats=> 3, ties=>0) The answer I seek in this case would be 2 as ties is zero. I don't want to know what the aggregate of the values are or other such info. I have tried looping through with a foreach loop testing for $value > 0 and I failed miserably Any ideas ? Many Thanks ! Quote Link to comment Share on other sites More sharing options...
xyph Posted May 9, 2012 Share Posted May 9, 2012 Let's see your foreach. Quote Link to comment Share on other sites More sharing options...
Barand Posted May 9, 2012 Share Posted May 9, 2012 try function posval($v) { return $v >= 1; } $a = array ( 'shoes' => 5, 'coats' => 3, 'ties' => 0, 'jackets' => '', 'socks' => null ); $posvals = count(array_filter($a, 'posval')) ; // 2 Quote Link to comment Share on other sites More sharing options...
leitning Posted May 9, 2012 Share Posted May 9, 2012 Are you sure the values you're evaluating are integers? Try: $posCount = 0; foreach($array as $value) { if( is_numeric($value) && intval($value) > 0 ) $posCount++; } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.