Skip to content
Home » How to search for integers in associative array PHP

How to search for integers in associative array PHP

Functions Mentioned in this Post

Associative arrays in PHP are versatile in the sense that they can store mixed data types as keys and values. Now if we have to filter out keys or values based on their data types, we have to search through the array to filter it. In this article, we’ll see a couple of options to search for integers in associative array PHP.

So, let’s head straight to the topic.

Option#1 – Search for integers in associative array using foreach loop

Using a foreach loop is the first thing that comes to mind when we have an array to loop through. Here we’ll see how to use the foreach loop to search integer keys and values.

Search for integer values in associative array using foreach loop

In this example, we’ll use a foreach loop to iterate over an array and filter out integer values.

<?php
$arr = array(
    0 => "Susan",
    1 => 2,
    5 => "Daniel",
    3 => 4,
    6 => "Kylie",
    7 => 100,
    8 => 2021,
    9 => "Richard",
    10 => 121
);
 
 
//Get the filtered response.
$filtered_arr = array();
 
//Loop through the values
foreach($arr as $key=>$value)
{
    //If the value is integer.
    if(gettype($value) == 'integer')
    {
        $filtered_arr[$key] = $value;
    } 
}
 
print_r($filtered_arr);
 
/*
OUTPUT
Array
(
    [1] => 2   
    [3] => 4   
    [7] => 100 
    [8] => 2021
    [10] => 121
)
*/
 
?>

PHP getype function returns the datatype of the value and if it is an integer we pass it to the filtered array. Finally, we print the final array and get the integer values along with their original keys.

Search for key values in associative array using foreach loop

Now, we can filter out the array based on the data type of the key values. Let’s see an array with key values of mixed data types.

<?php
$arr = array(
    0 => "Susan",
    "One" => 2,
    5 => "Daniel",
    "Three" => 4,
    6 => "Kylie",
    7 => 100,
    "Eight" => 2021,
    9 => "Richard",
    "Ten" => 121
);
 
 
//Get the filtered response.
$filtered_arr = array();
 
//Loop through the values
foreach($arr as $key=>$value)
{
    //If the value is integer.
    if(gettype($key) == 'integer')
    {
        $filtered_arr[$key] = $value;
    } 
}
 
print_r($filtered_arr);
 
/*
OUTPUT
Array
(
    [0] => Susan  
    [5] => Daniel 
    [6] => Kylie  
    [7] => 100    
    [9] => Richard
)
*/
 
?>

With a slight change, the code filters out the integer key in an array. Great! Let’s see another very sophisticated method of searching integers in associative arrays.

Option#2 – Search for integers in associative array using array_filter

PHP array_filter function gets a callback function and based on the return value, it filters out an array. We’ll use array_filter for searching integers in an associative array. But before that, let’s see the anatomy of this function.

Description

Filters array elements using a callback function

Function Signature

array_filter(array $array, ?callable $callback = null, int $mode = 0): array

Arguments

  • $array – The array to filter
  • $callback – A user-defined function
  • $mode –  Flag that determines the callback function’s parameter

Note

The $mode accepts the following flag values. 

  • ARRAY_FILTER_USE_KEY – pass key as the only argument to callback instead of the value
  • ARRAY_FILTER_USE_BOTH – pass both value and key as arguments to callback instead of the value

By default, the function passes only the key values to the callback function.

Return Type

The function returns a filtered array.

Search for integer values in associative array using array_filter

We’ll define a callback function named, isInt. The function takes values and returns true if the type is an integer. The integer values are thus filtered out to the output array.

<?php
$arr = array(
    0 => "Susan",
    1 => 2,
    5 => "Daniel",
    3 => 4,
    6 => "Kylie",
    7 => 100,
    8 => 2021,
    9 => "Richard",
    10 => 121
);
 
 
//User defined callback function
function isInt($value)
{
    return gettype($value) == 'integer';
}
 
//Filter array based on callback function isInt
$filtered_arr = array_filter($arr,"isInt");
 
 
print_r($filtered_arr);
 
/*
OUTPUT
Array
(
    [1] => 2   
    [3] => 4   
    [7] => 100 
    [8] => 2021
    [10] => 121
)
*/
 
?>

Quite neat!! The user defined function identifies integer values and filters them out to an output array.

Search for integer keys in associative array using array_filter

Now, let’s look for integer keys using array_filter. We’ll pass ARRAY_FILTER_USE_KEY as $mode function argument. With this change, the function will pass keys to the callback function instead of values.

<?php
$arr = array(
    0 => "Susan",
    "One" => 2,
    5 => "Daniel",
    "Three" => 4,
    6 => "Kylie",
    7 => 100,
    "Eight" => 2021,
    9 => "Richard",
    "Ten" => 121
);
 
 
//User defined callback function
function isInt($key)
{
    return gettype($key) == 'integer';
}
 
//Filter array based on callback function isInt
$filtered_arr = array_filter($arr,"isInt",ARRAY_FILTER_USE_KEY);
 
 
print_r($filtered_arr);
 
/*
OUTPUT
Array
(
    [0] => Susan  
    [5] => Daniel 
    [6] => Kylie  
    [7] => 100    
    [9] => Richard
)
*/
 
 
?>

See, how convenient. The function is a one-liner that accomplishes filtering operations effectively. 

Conclusion

We’ve seen a couple of options to search for integers in associative array in PHP. The first option includes a foreach loop while the second option uses a more sophisticated array_filter approach. So, we hope you’ve learned something new today. With this, the article comes to the end. Stay tuned for more interesting articles about PHP.

Want to explore more useful PHP tutorials?

We have many fun articles related to PHP. You can explore these to learn more about PHP.

How to sort an associative array in PHP

PHP serialize vs json_encode

How to take form inputs and put them into a PHP associative array