PHP array_filter() function filters elements of an array by a callback function and it can be used for many purposes. The array_filter() function provides a short and simple way to filter multidimensional array by key and value.
In the following example code, we will show you how to filter values from multidimensional array similar to SQL LIKE using the array_filter() function in PHP. Using this code, you can filter multidimensional arrays by specific key and value.
$array = array(
array('name' => 'John Doe', 'email' => 'john@gmail.com'),
array('name' => 'Marry Lies', 'email' => 'marry@gmail.com'),
array('name' => 'Andrew Joe', 'email' => 'andrew@gmail.com'),
);
$like = 'jo';
$result = array_filter($array, function ($item) use ($like) {
if (stripos($item['name'], $like) !== false) {
return true;
}
return false;
});
The above code returns the filtered array.
Array ( [0] => Array ( [name] => John Doe [email] => john@gmail.com ) [2] => Array ( [name] => Andrew Joe [email] => andrew@gmail.com ) )
Great! Thanks!
Nice work, made my function super easy and readable. Thanks!
Nice solution
On my registration form i want to
include 3 local governments out of
out which the user will select 1 by use of radio
Input type. Each local govt will have towns
of Origins. What i want is the code or script
that the town chosen is not in your selected local
Govt.
$ws =”warri south”
$wn =”warri north”
$wsw =”warri southwest”
$wsa = array ( ‘a’ , ‘b’ ,’c’): // towns in warri south
$wna = array ( ‘j’ , ‘k’ ,’l’): // towns in warri north
$wswa =array( ‘x’ , ‘y’ , ‘z’) // towns in warri southwest
If the user should select warri south as his local govt
then selects ‘y’ as his town of origin, i would want the
Script to alert him that the the town selected is not
in that local government area.
Pls assist me with the right code. Thank you.
Thanks , it helped me lot
Thank you!! this helped me