
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
How can I count true and false values in my PHP array?
Let’s say the following is our array −
$isMarriedDetails = [ false, true, false, true, true, false, false, true, false ];
To count true and false values from an array, at first, count total values and subtract the number of true results. In this way, you will get the number of false values and same for true.
Example
<!DOCTYPE html> <html> <body> <?php $isMarriedDetails = [ false, true, false, true, true, false, false, true, false ]; $trueResult = count(array_filter($isMarriedDetails)); $falseResult = count($isMarriedDetails) - $trueResult; echo "Number of false value=",$falseResult,"<br>"; echo "Number of true value=",$trueResult; ?> </body> </html>
Output
This will produce the following output
Number of false value=5 Number of true value=4
- Related Articles
- In MySQL, without having BOOLEAN data type how can we show TRUE and FALSE values?
- Get the number of true/false values in an array using JavaScript?
- How to display array values with do while or for statement in my PHP?
- How can I increase my monthly savings?
- How can I curb my shopping addiction?
- How can I improve my spoken English?
- How can I get my invention patented?
- How to count values from a PHP array and show value only once in a foreach loop?
- How can I add textures to my bars and wedges in Matplotlib?
- How can I remove all empty values when I explode a string using PHP?
- How can I remove dandruff from my hair?
- How can I make my custom objects Parcelable?
- How can I make my custom objects Serializable?
- How can I make my layout scroll vertically?
- How can I get enum possible values in a MySQL database using PHP?

Advertisements