note 72860 modified in function.count by danbrown

From: Date: Fri, 09 Jan 2009 23:00:12 +0000
Subject: note 72860 modified in function.count by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-149011@lists.php.net to get a copy of this message
<?php // countValuesRecursive // The goal of this function is to count non-false values of a multidimenional array // This is useful in making a quick determination if a form sent any values // If no values were sent I can simply return to the blank form rather than continuing to the validation of each input // There are two limitations of the principle: // 1. If you WANT to send FALSE, 0, '', or NULL as form values this function will not count those, thus not doing what's expected // 2. This would create an endless loop on a form that has no required fields such as one where users can choose to recieve optional email subscriptions but where choosing none is also valid function countValuesRecursive($array, $count = 0) { // Cycle through the array foreach ($array as $value) { // Check if the value is an array if (is_array($value)) { // Cycle through deeper level $count = countValuesRecursive($value, $count); } else { // Check if the value is TRUE if ($value) { $count++; } } } // Return the count return $count; } ?> --was-- // countValuesRecursive // The goal of this function is to count non-false values of a multidimenional array // This is useful in making a quick determination if a form sent any values // If no values were sent I can simply return to the blank form rather than continuing to the validation of each input // There are two limitations of the principle: // 1. If you WANT to send FALSE, 0, '', or NULL as form values this function will not count those, thus not doing what's expected // 2. This would create an endless loop on a form that has no required fields such as one where users can choose to recieve optional email subscriptions but where choosing none is also valid function countValuesRecursive($array, $count = 0) { // Cycle through the array foreach ($array as $value) { // Check if the value is an array if (is_array($value)) { // Cycle through deeper level $count = countValuesRecursive($value, $count); } else { // Check if the value is TRUE if ($value) { $count++; } } } // Return the count return $count; } http://php.net/manual/en/function.count.php

« previous php.notes (#149011) next »