Diafol's Demos

Back To Index

<demo>Filter Arrays with Parameters</demo>

A demo for filtering array data with array_filter(), using an anonymous function (or 'closure') as a callback. This avoids the need to loop over the array.

Supply Your Data

In this example we'll use the mb_substr() function to filter the data from the textarea. Place each item of the original array on a separate row of the textarea, and decide on a substring for which to search and the start position of the substring.

This is just a simple demo for using parameters with array_filter(), so not to worry about there being alternative methods of matching a substring at a particular position.

PHP Code

Output of Filtered Array ($newarray)

General Code

$variable1 = 'some value';
$variable2 = 'some other value';

$array = array(... values ...);

$newarray = array_filter($array, function($elem) use ($variable1,$variable2){ 
			return someTestUsingTheVariables;