🌐
PHP
php.net › manual › en › function.array-filter.php
PHP: array_filter - Manual
Returns the filtered array. ... <?php function odd($var) { // returns whether the input integer is odd return $var & 1; } function even($var) { // returns whether the input integer is even return !($var & 1); } $array1 = ['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5]; $array2 = [6, 7, 8, 9, 10, 11, 12]; echo "Odd :\n"; print_r(array_filter($array1, "odd")); echo "Even:\n"; print_r(array_filter($array2, "even")); ?>
🌐
W3Schools
w3schools.com › php › func_array_filter.asp
PHP array_filter() Function
The array_filter() function filters the values of an array using a callback function.
Discussions

PHP Array_filter on "simple" multi-level array - Stack Overflow
Can I and how can I use PHP's array_filter to filter the blank/nulls entries out of the following array structure? From: The array is from a PDO call using Fetch BOTH so the numeric and named valu... More on stackoverflow.com
🌐 stackoverflow.com
Can someone ELI5 the Javascript array.filter() method?
Filter takes in a predicate (A function that returns true or false) that is run on each element of the array. All elements that returned true are kept, and false ones are thrown out. Example a = [1,2,3,4,5]; b = [2,4]; result = a.filter(function (x) {return b.indexOf(x) == -1}); Here we are defining an anonomous function that returns true if the index of the element in b is -1 (ie it is not in b). When it is not in b, it returns true so that the filter keeps those elements. More on reddit.com
🌐 r/learnprogramming
5
2
September 1, 2015
Is there a PHP equivalent of Javascript's Array.find?

Why don't you make your cities array indexed by id?

More on reddit.com
🌐 r/PHPhelp
14
5
January 8, 2016
Searching and filtering PHP arrays to later query a database

You have one record per value in your query result, and your array_count_values array gives you the count per record already indexed by your lookup column. Wouldn't something like this give you what you want?

$row = $result->fetch_assoc();
$row['valueToMultiply'] *= $multiSrvArray['columnName'];
More on reddit.com
🌐 r/PHPhelp
2
5
March 20, 2010
🌐
W3Schools
w3schools.com › php › php_looping_for.asp
PHP for loop
connection_aborted() connection_status() connection_timeout() constant() define() defined() die() eval() exit() get_browser() __halt_compiler() highlight_file() highlight_string() hrtime() ignore_user_abort() pack() php_strip_whitespace() show_source() sleep() sys_getloadavg() time_nanosleep() time_sleep_until() uniqid() unpack() usleep() PHP MySQLi · affected_rows autocommit change_user character_set_name close commit connect connect_errno connect_error data_seek debug dump_debug_info errno error error_list fetch_all fetch_array fetch_assoc fetch_field fetch_field_direct fetch_fields fetch_l
🌐
Medium
inyomanjyotisa.medium.com › understanding-the-php-array-filter-function-864a29fbd746
Understanding the PHP array_filter() Function | by I Nyoman Jyotisa | Medium
January 2, 2025 - The array_filter() function in PHP is a versatile tool for filtering arrays based on a user-defined condition. By applying a callable function (such as a closure) to each element, it selectively includes elements that meet the specified criteria.
🌐
AlphaCodingSkills
alphacodingskills.com › php › notes › php-array-filter.php
PHP array_filter() Function - AlphaCodingSkills
The PHP array_filter() function filters elements of an array using a callback function. This function iterates over each value in the array passing them to the callback function.
🌐
Altorouter
altorouter.com › home › mastering php array filter in just an hour
PHP Array Filter: Unraveling Secrets
January 22, 2024 - The array_filter() function in PHP comprises the power to sieve elements of an array based on a particular criteria defined in a callback function. For each element in the input array, the function applies the callback function.
Find elsewhere
🌐
Designcise
designcise.com › web › tutorial › how-to-use-the-php-array-filter-function-to-filter-by-keys
Use PHP array_filter() to Filter by Keys - Designcise
August 6, 2021 - $arr = [ 'foo' => 'bar', 1 => 'baz', 'qux' => 22, 3 => 123, 'quux' => 'quuz', ]; $filteredArr = array_filter( $arr, fn ($key) => is_numeric($key), ARRAY_FILTER_USE_KEY ); echo print_r($filteredArr, true); // [1 => 'baz', 3 => 123]
🌐
W3Schools
w3schools.com › php › php_oop_constructor.asp
PHP OOP Constructor Method
Arrays Indexed Arrays Associative ... Arrays Array Functions PHP Superglobals · Superglobals $GLOBALS $_SERVER $_REQUEST $_POST $_GET PHP RegEx PHP RegEx Functions · PHP Form Handling PHP Form Validation PHP Form Required PHP Form URL/E-mail PHP Form Complete · PHP Date and Time PHP Include PHP File Handling PHP File Open/Read PHP File Create/Write PHP File Upload PHP Cookies PHP Sessions PHP Filters PHP Filters ...
🌐
Php
php.ru › manual › function.array-filter.html
array_filter - Фильтрует элементы массива с помощью callback-функции | Руководство по PHP | PHP.RU
array_filter (PHP 4 >= 4.0.6, PHP 5, PHP 7) array_filter — Фильтрует элементы массива с помощью callback-функции Описание array array_filter ( array $array ] ) Обходит каждое
🌐
ZetCode
zetcode.com › php-array › array-filter
PHP array_filter - Array Filtering in PHP
March 13, 2025 - The PHP array_filter function filters elements of an array using a callback function.
🌐
Medium
medium.com › @hbahonar › php-array-filter-function-with-practical-examples-a89b8f2be7c9
PHP array_filter() Function With Practical Examples | by H Bahonar | Medium
May 24, 2023 - PHP array_filter() is a built-in PHP function that filters elements of an array using a built-in PHP function or a custom user-defined callback function.
Top answer
1 of 4
2

I think this can not be done using only array_filter function because sometimes you need to modify array elements but the array_filter function allows only to decide if the element should be excluded or not.

For example in the main array element with index 2400 should be included in the result set but it's content should be modified.

I wrote a simple function to do this, hope it might help. Well, you might use this for inspiration. And it was interesting challenge for me as well.

Below is my function with couple tests.

Copy<?php

function deepFilter(array $array)
{
    // Formally this is not need because if array is empty then $filteredArray will also be empty
    // but it simplifies the algorithm
    if (empty($array)) {
        return [];
    }

    $filteredArray = [];
    foreach ($array as $key => $value) {
        if (is_array($value) && !empty($value)) {
            $value = deepFilter($value);
        }
        if (!empty($value)) {
            $filteredArray[$key] = $value;
        }
    }

    return $filteredArray;
}

$testArray1 = [
    2400 => [
        0 => [
            'value' => 7,
            0 => 7,
        ],
        1 => [
            'value' => 61,
            0 => 61,
        ],
        2 => [
            'value' => 42,
            0 => 42,
        ],
        3 => [
            'value' => null,
            0 => null,
        ]
    ]
];

$testArray2 = [
    2400 => [
        0 => [
            'value' => 7,
            0 => 7,
        ],
        1 => [
            'value' => 61,
            0 => 61,
        ],
        2 => [
            'value' => 42,
            0 => 42,
        ],
        3 => null
    ],
    3243 => [
        0 => [
            'value' => 7,
            0 => null,
        ],
        1 => [
            'value' => null,
            0 => 61,
        ],
        2 => [
            'value' => 42,
            0 => 42,
        ],
        3 => null
    ]
];
var_export(deepFilter($testArray1));
var_export(deepFilter($testArray2));

The idea is very simple.

  1. Take an array and check elements one by one.
  2. If element is an array, apply the function for that element and check the result. We can remove everything from child array and in this case we should not add it to results. Else if child has something remaining after cleanup include 'cleaned child' in our result set.
  3. If our element is not an array then include it only if it's not empty.

Please let me know if you find any mistakes or if it works for you or not.

2 of 4
2

Use array_filter and test the value element (or the 0 element, since they're equivalent).

Copy$array[2400] = array_filter($array[2400], function($element) {
    return $element['value'];
});

To do it for all elements of the outer array, use a foreach loop.

Copyforeach ($array as &$subarray) {
    $subarray = array_filter($subarray, function($element) {
        return $element['value'];
    });
}

or array_map:

Copy$array = array_map(function($subarray) {
    return array_filter($subarray, function($element) {
        return $element['value'];
    });
}, $array);
🌐
Honar Systems
honarsystems.com › home › php array filter function
PHP Array Filter Function
August 3, 2025 - The PHP array_filter() function is used to filter elements from an array by key or value, or a custom user-defined callback function.
🌐
Tutorialspoint
tutorialspoint.com › php › php_function_array_filter.htm
PHP - Function array_filter()
It returns the filtered array. ... <?php function odd($var) { return($var & 1); } function even($var) { return(!($var & 1)); } $input1 = array("a"=>1, "b"=>2, "c"=>3, "d"=>4, "e"=>5); $input2 = array(6, 7, 8, 9, 10, 11, 12); echo "Odd Values:\n"; print_r(array_filter($input1, "odd")); echo "Even Values:\n"; print_r(array_filter($input2, "even")); ?>
🌐
Laravel
laravel.com › docs › 12.x › database
Database: Getting Started - Laravel 12.x - The PHP Framework For Web Artisans
'mysql' => [ 'driver' => 'mysql', 'read' => [ 'host' => [ '192.168.1.1', '196.168.1.2', ], ], 'write' => [ 'host' => [ '192.168.1.3', ], ], 'sticky' => true, 'port' => env('DB_PORT', '3306'), 'database' => env('DB_DATABASE', 'laravel'), 'username' => env('DB_USERNAME', 'root'), 'password' => env('DB_PASSWORD', ''), 'unix_socket' => env('DB_SOCKET', ''), 'charset' => env('DB_CHARSET', 'utf8mb4'), 'collation' => env('DB_COLLATION', 'utf8mb4_unicode_ci'), 'prefix' => '', 'prefix_indexes' => true, 'strict' => true, 'engine' => null, 'options' => extension_loaded('pdo_mysql') ? array_filter([ (PHP_VERSION_ID >= 80500 ?
🌐
GeeksforGeeks
geeksforgeeks.org › php › php-array_filter-function
PHP array_filter() Function - GeeksforGeeks
June 20, 2023 - This built-in function in PHP is used to filter the elements of an array using a user-defined function which is also called a callback function. The array_filter() function iterates over each value in the array, passing them to the user-defined ...
🌐
Reintech
reintech.io › blog › mastering-php-array-filter-function-for-filtering-arrays
Mastering PHP's `array_filter()` Function for Filtering Arrays | Reintech media
April 14, 2023 - PHP's array_filter() function stands as one of the most versatile tools in the language's array manipulation arsenal. Unlike its procedural counterparts, this function enables declarative data filtering through callback functions, making your ...
🌐
Ub
cs.ub.bw › teaching › teachings › csi223 › php › function.array-filter.html
array_filter
(PHP 4 >= 4.0.6, PHP 5)array_filter -- Filters elements of an array using a callback function