In PHP, handling arrays is a common task, particularly during data cleaning, filtering, and transformation. When you need to extract all integer values from a mixed array, it can pose some challenges. Fortunately, PHP offers powerful tools that make this process efficient and straightforward. This article demonstrates how to filter integers from an array with ease using a combination of is_int and array_filter.
The array_filter function is used to iterate through an array and decide whether to keep each element based on a callback function. Its syntax is as follows:
<span><span><span class="hljs-title function_ invoke__">array_filter</span></span><span>(</span><span><span class="hljs-keyword">array</span></span><span> </span><span><span class="hljs-variable">$array</span></span>, ?</span><span><span class="hljs-keyword">callable</span></span> </span><span><span class="hljs-variable">$callback</span></span> = </span><span><span class="hljs-literal">null</span></span>, </span><span><span class="hljs-keyword">int</span></span> </span><span><span class="hljs-variable">$mode</span></span> = </span><span><span class="hljs-number">0</span></span>): </span><span><span class="hljs-keyword">array</span></span>
</span></span>
$array: The array to be processed.
$callback: A callback function that filters array elements. If the callback returns true, the element is kept; if it returns false, the element is discarded.
$mode: The filtering mode, which determines the parameters passed to the callback.
If no $callback parameter is provided, array_filter will by default remove elements with a value of false (including empty strings, 0, null, etc.).
is_int is a built-in PHP function that checks whether a variable is an integer. Its syntax is as follows:
<span><span><span class="hljs-title function_ invoke__">is_int</span></span><span>(</span><span><span class="hljs-keyword">mixed</span></span> </span><span><span class="hljs-variable">$var</span></span>): </span><span><span class="hljs-keyword">bool</span></span>
</span></span>
$var: The variable to check.
Return value: Returns true if $var is an integer; otherwise returns false.
The simplest way to filter all integers in an array is to combine is_int with array_filter. You just need to provide a callback function that calls is_int on each array element.
<span><span><span class="hljs-meta"><?php</span></span>
<p></span>// Define a mixed array<br>
</span>$array = [</span>1, </span>'apple', </span>2, </span>3.5, </span>4, </span>'banana', </span>5, </span>null];</p>
<p></span>// Use array_filter with is_int to extract integers<br>
</span>$integers = </span>array_filter(</span>$array, </span>'is_int');</p>
<p></span>// Output result<br>
</span>print_r(</span>$integers);</p>
<p></span>?><br>
</span></span>
Output:
<span><span><span class="hljs-title function_ invoke__">Array</span></span>
(
[</span><span><span class="hljs-number">0</span></span>] => </span><span><span class="hljs-number">1</span></span>
[</span><span><span class="hljs-number">2</span></span>] => </span><span><span class="hljs-number">2</span></span>
[</span><span><span class="hljs-number">4</span></span>] => </span><span><span class="hljs-number">4</span></span>
[</span><span><span class="hljs-number">6</span></span>] => </span><span><span class="hljs-number">5</span></span>
)
</span></span>
In this example, array_filter iterates through $array and applies is_int to each element. Only when an element is an integer does is_int return true, and then array_filter retains it.
If the array contains 0 or false, these values will not be filtered out by array_filter, unless you explicitly tell it to. If your concern is only the type (integers) and not the actual value, you can safely continue using is_int.
If you also want to filter out floats (e.g., 3.5), you can adjust the callback logic accordingly. For example, use is_float to further filter values.
<span><span><span class="hljs-variable">$filtered</span></span> = </span><span><span class="hljs-title function_ invoke__">array_filter</span></span>(</span><span><span class="hljs-variable">$array</span></span>, function(</span><span><span class="hljs-variable">$value</span></span>) {
</span><span><span class="hljs-keyword">return</span></span> </span><span><span class="hljs-title function_ invoke__">is_int</span></span>(</span><span><span class="hljs-variable">$value</span></span>) && !</span><span><span class="hljs-title function_ invoke__">is_float</span></span>(</span><span><span class="hljs-variable">$value</span></span>);
});
</span></span>
array_filter is inherently efficient and can handle large datasets. If you need to process a large array, ensure that the callback logic (such as is_int) remains as simple as possible, since the callback is executed on every iteration.
If you require even better performance, you can use a native foreach loop to manually filter the array, avoiding the overhead of array_filter:
<span><span><span class="hljs-meta"><?php</span></span>
<p></span>$integers = [];<br>
</span>foreach (</span>$array </span>as </span>$item) {<br>
</span>if (</span>is_int(</span>$item)) {<br>
</span>$integers[] = </span>$item;<br>
}<br>
}</p>
<p></span>print_r(</span>$integers);</p>
<p></span>?><br>
</span></span>
By combining is_int and array_filter, we can efficiently filter out all integer values from an array. This method is ideal for scenarios requiring quick data cleaning and filtering. While array_filter offers concise syntax, if you are working with extremely large arrays, a foreach loop may deliver better performance. Overall, choosing the right tool and method helps improve both code efficiency and readability.
Related Tags:
array_filter