note 72860 deleted from function.count by levim
| From: | levim@php.net | Date: | Thu, 10 Jan 2013 14:22:27 +0000 |
| Subject: | note 72860 deleted from function.count by levim | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-192879@lists.php.net to get a copy of this message | ||
Note Submitter: Colin
----
<?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;
}
?>