In PHP, the is_bool function can be used to check whether a variable is a boolean, while the array_filter function filters elements of an array using a specified callback function. By combining these two, we can easily extract boolean values from an array.
is_bool(): Checks whether a given variable is of boolean type. It returns true if the variable is a boolean (true or false), otherwise it returns false.
<span><span><span class="hljs-title function_ invoke__">var_dump</span></span><span>(</span><span><span class="hljs-title function_ invoke__">is_bool</span></span><span>(</span><span><span class="hljs-literal">true</span></span><span>)); </span><span><span class="hljs-comment">// bool(true)</span></span><span>
</span><span><span class="hljs-title function_ invoke__">var_dump</span></span><span>(</span><span><span class="hljs-title function_ invoke__">is_bool</span></span><span>(</span><span><span class="hljs-number">1</span></span><span>)); </span><span><span class="hljs-comment">// bool(false)</span></span><span>
</span></span>
array_filter(): This function is used to filter elements of an array. It takes two parameters: the array to filter and a callback function. The callback function determines which elements are kept and which are removed. If the callback returns true, the element is kept; if it returns false, the element is removed.
<span><span><span class="hljs-variable">$arr</span></span><span> = [</span><span><span class="hljs-number">1</span></span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>];
</span><span><span class="hljs-variable">$filtered</span> = <span class="hljs-title function_ invoke__">array_filter</span>(<span class="hljs-variable">$arr</span>, function(<span class="hljs-variable">$value</span>) {
return <span class="hljs-variable">$value</span> > <span class="hljs-number">2</span>;
});
<span class="hljs-title function_ invoke__">print_r</span>(<span class="hljs-variable">$filtered</span>); // Output [3, 4, 5]
</span></span>
By combining is_bool with array_filter, we can filter out all boolean values from an array. A simple callback function can be used to check whether each element is a boolean, and if so, keep it.
<span><span><span class="hljs-meta"><?php</span>
<span class="hljs-comment">// Example array</span>
<span class="hljs-variable">$array</span> = [true, false, 1, "hello", null, true, 0, false];
<p>// Use array_filter and is_bool together to filter boolean values<br>
$filteredArray = array_filter($array, function($value) {<br>
return is_bool($value); // Keep only booleans<br>
});</p>
<p>// Print results<br>
print_r($filteredArray);<br>
?><br>
</span></span>
<span>Array
(
[0] => 1
[1] =>
[5] => 1
[7] =>
)
</span>
In this example, array_filter iterates through all elements of the $array, and only those that are booleans (true or false) are retained. Note that array_filter preserves the original array keys.
By default, array_filter preserves the original keys of the array. If you want the filtered array to have consecutive keys, you can use the array_values function to reindex the array.
<span><span class="hljs-meta"><?php</span>
<span class="hljs-variable">$array</span> = [true, false, 1, "hello", null, true, 0, false];
<p>// Filter booleans and reindex keys<br>
$filteredArray = array_values(array_filter($array, function($value) {<br>
return is_bool($value); // Keep only booleans<br>
}));</p>
<p>// Print results<br>
print_r($filteredArray);<br>
?><br>
</span></span>
<span>Array
(
[0] => 1
[1] =>
[2] => 1
[3] =>
)
</span>
By using array_values, we get a new array with reindexed keys starting from 0, while still keeping all boolean values.
By combining is_bool and array_filter, we can conveniently extract all boolean values from an array. array_filter provides flexible filtering functionality, while is_bool accurately determines whether an element is of boolean type. This approach is especially useful in scenarios where precise filtering of boolean values is required when handling data.
In real-world development, mastering the combined use of these built-in functions will help you efficiently process array data.
Related Tags:
array_filter