ceci Posted April 9, 2010 Share Posted April 9, 2010 Hi. echo '<pre>'. print_r($items, true).'</pre>'; The statement above returns the following array: Array ( [1] => 325 [2] => 326 [3] => 327 [4] => [5] => [6] => [7] => [8] => [9] => [10] => [11] => [12] => ) echo COUNT($items) returns: 12 The count should be 3 not 12. How can I exclude the empty array? thanks Quote Link to comment https://forums.phpfreaks.com/topic/198138-count-function-returns-wrong-count/ Share on other sites More sharing options...
Ken2k7 Posted April 9, 2010 Share Posted April 9, 2010 count is fine because an empty string still counts as an entry in an array. You would want a loop or a loop function like array_walk. Quote Link to comment https://forums.phpfreaks.com/topic/198138-count-function-returns-wrong-count/#findComment-1039583 Share on other sites More sharing options...
ceci Posted April 9, 2010 Author Share Posted April 9, 2010 I want to be able to quickly do a loop using a simple count like this: for($i = 0; $i < count($items); $i++) { // do things here. } I tried using the unset() function but it does not seem to do anything. for($j=1; $j <= count($items); $j++){ if($items[$j]==NULL){ unset($items[$j]); } } There has to be an easy way to do this. Quote Link to comment https://forums.phpfreaks.com/topic/198138-count-function-returns-wrong-count/#findComment-1039599 Share on other sites More sharing options...
JustLikeIcarus Posted April 9, 2010 Share Posted April 9, 2010 $item_count = 0; while (list($key, $value) = each($items)) { if ($value) { $item_count++; } } for($i = 0; $i < $item_count; $i++) { // do things here. } Quote Link to comment https://forums.phpfreaks.com/topic/198138-count-function-returns-wrong-count/#findComment-1039602 Share on other sites More sharing options...
ddubs Posted April 9, 2010 Share Posted April 9, 2010 $i = 0; foreach($items as $key => $value) { if(!empty($value)) { $i++; } } To create an new array, do this: foreach($items as $key => $value) { if(!empty($value)) { $newArray[] = $value; } } $i will have your total. You may also want to look into why your array is getting NULL values entered into it as well... Quote Link to comment https://forums.phpfreaks.com/topic/198138-count-function-returns-wrong-count/#findComment-1039608 Share on other sites More sharing options...
Ken2k7 Posted April 9, 2010 Share Posted April 9, 2010 I'm generally not a fan of looping through the array that many times when you don't need the empty values. Or, just remove the empty ones first: <?php function clearEmptyValues ($arr) { $newarr = array(); foreach ($arr as $key => $val) { if (!empty($v)) $newarr[$key] = $val; } return $newarr; } Then: $items = clearEmptyValues($items); $count = count($items); Quote Link to comment https://forums.phpfreaks.com/topic/198138-count-function-returns-wrong-count/#findComment-1039614 Share on other sites More sharing options...
ceci Posted April 9, 2010 Author Share Posted April 9, 2010 I used your functions and made it work. Thank you everyone! C Quote Link to comment https://forums.phpfreaks.com/topic/198138-count-function-returns-wrong-count/#findComment-1039639 Share on other sites More sharing options...
Ken2k7 Posted April 9, 2010 Share Posted April 9, 2010 Oops I spotted a typo. Typed too fast. Good catch ceci. =] Quote Link to comment https://forums.phpfreaks.com/topic/198138-count-function-returns-wrong-count/#findComment-1039651 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.