array_filter

Description

Iterates over each value in the array
passing them to the callback function.
If the callback function returns true, the
current value from array is returned into
the result array. Array keys are preserved.

Parameters

array

The array to iterate over

callback

The callback function to use

If no callback is supplied, all entries of
array equal to FALSE (see
converting to
boolean) will be removed.

flag

Flag determining what arguments are sent to callback:

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

Because array_filter() preserves keys, you should consider the resulting array to be an associative array even if the original array had integer keys for there may be holes in your sequence of keys. This means that, for example, json_encode() will convert your result array into an object instead of an array. Call array_values() on the result array to guarantee json_encode() gives you an array.

Functional programming is a paradigm which centers around the side-effect free evaluation of functions. A program execution is a call of a function, which in turn might be defined by many other functions. One idea is to use functions to create special purpose functions from other functions.

The array functions mentioned above allow you compose new functions on arrays.

E.g. array_sum = array_map("sum", $arr).

This leads to a style of programming that looks much like algebra, e.g. the Bird/Meertens formalism.

E.g. a mathematician might state

map(f o g) = map(f) o map(g)

the so called "loop fusion" law.

Many functions on arrays can be created by the use of the foldr() function (which works like foldl, but eating up array elements from the right).

I can't get into detail here, I just wanted to provide a hint about where this stuff also shows up and the theory behind it.

Although it states clearly that array keys are preserved, it's important to note this includes numerically indexed arrays. You can't use a for loop on $array above without processing it through array_values() first.

I needed to filter specific files. glob was a good option but had issues being case sensitive. And at the same time, I had to filter the directory names like ".", "..", ".git", etc which could be complex.

I used a snippet as below, where I could implement this complex filter. Just modify your method: image_file.

If given an integer this function returns the integer 1 if $var is odd and the integer 0 if $var is even.The single ampersand, &, is the bitwise AND operator. The way it works is that it takes the binary representation of the two arguments and compare them bit for bit using AND. If $var = 45, then since 45 in binary is 101101 the operation looks like this:

45 in binary: 1011011 in binary: 000001 ------result: 000001

Only if the last bit in the binary representation of $var is changed to zero (meaning that the value is even) will the result change to 000000, which is the representation of zero.

Read "callback" parameter note with understanding (as well as "converting to boolean" chapter). Keep in midn, that 0, both:

* integer: 0 and* float: 0.00

evaluates to boolean FALSE! And therefore all array nodes, that have such value WILL ALSO BE FILTERED by array_filter(), with default call back. Unless you provide your own callback function, that will (for example) filter only empty strings and NULLs, but leave "zeros" untouched.

If you have not noticed already - array_filter() can be used to remove empty elements, since an empty string considered "false", if you not specify a callback

Keep in mind, that this will remove also some other values - so if you want a quick "remove empty elements from array" this function will be fine, as long as you dont have anything to keep, which casts to "false"

If you're using filter_input_array, the values will be null on failure and anything else on success. Because array_filter by default removes false, 0 and "" you need to do extra work like this:<?php$input_array = filter_input_array(INPUT_GET, array('var1' => FILTER_VALIDATE_BOOLEAN,'var2' => FILTER_VALIDATE_INT));

Without using ksort, the keys appear before the fallback values, instead of inline and appearing like they replace them, however the keys are intact (which is why ksort works) so it's only execution order.

if needle is left empty, the function will delete the array members that have no value (this means if it's empty).
NOTE: It rebuilds the array from scratch, so keys begin with 0, like you would create a new array.

I was looking for a function to delete values from an array and thought I had found it in array_filter(), however, I *didn't* want the keys to be preserved *and* I needed blank values cleaned out of the array as well. I came up with the following (with help from many of the above examples):

Here is a nice little function which will apply a callback function recursively over a multidimensional array. If the callback function returns false, then it replaces the value of the array with $filtered_ouput. This function gracefully handles objects inside of arrays (and objects within objects within arrays, etc). It is specifically designed for your callback function to process on the array key's (unlike normal array_filter which filters on the values), but it could work on the array values depending on your test criteria (YMMV).

This function trims empty strings from the beginning and end of an array.
It's useful when outputing plaintext files on a page and you want to skip empty lines at the beginning and end, but not within the text.

If you have a form with multiple checkbox having ID element as value for selection in a list, you probably have a SQL request like :$req="SELECT ... WHERE ID IN (".implode(',', array_keys($choices)).")";without quote before and after choices keys because they are numeric values... but in fact, they can be string values, and a SQL injection problem.