note 72860 added to function.count
| From: | Colin at osu1 dot php dot net | Date: | Sat, 03 Feb 2007 15:24:48 +0000 |
| Subject: | note 72860 added to function.count | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-122671@lists.php.net to get a copy of this message | ||
// 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;
}
----
Server IP: 64.71.164.2
Probable Submitter: 71.234.177.249
----
Manual Page -- http://www.php.net/manual/en/function.count.php
Edit -- https://master.php.net/note/edit/72860
Del: integrated -- https://master.php.net/note/delete/72860/integrated
Del: useless -- https://master.php.net/note/delete/72860/useless
Del: bad code -- https://master.php.net/note/delete/72860/bad+code
Del: spam -- https://master.php.net/note/delete/72860/spam
Del: non-english -- https://master.php.net/note/delete/72860/non-english
Del: in docs -- https://master.php.net/note/delete/72860/in+docs
Del: other reasons-- https://master.php.net/note/delete/72860
Reject -- https://master.php.net/note/reject/72860
Search -- https://master.php.net/manage/user-notes.php