PHP array_filter with arguments
As an alternative to @Charles's solution using closures, you can actually find an example in the comments on the documentation page. The idea is that you create an object with the desired state ($num
) and the callback method (taking $i
as an argument):
class LowerThanFilter {
private $num;
function __construct($num) {
$this->num = $num;
}
function isLower($i) {
return $i < $this->num;
}
}
Usage (demo):
$arr = array(7, 8, 9, 10, 11, 12, 13);
$matches = array_filter($arr, array(new LowerThanFilter(12), 'isLower'));
print_r($matches);
As a sidenote, you can now replace LowerThanFilter
with a more generic NumericComparisonFilter
with methods like isLower
, isGreater
, isEqual
etc. Just a thought — and a demo...
php array filter with custom arguments
Use an anonymous function instead of a defined function if you want to pass values into the scope of the callback.
array_filter($your_array, function($element) use ($action, $url) {
return isset($element->action, $element->url)
&& $element->action == $action
&& $element->url == $url;
});
PHP array_filter with argument to callback
I got it to work using an object, http://www.php.net/manual/en/language.pseudo-types.php#language.types.callback
class univFilter {
public $univ_seg;
public function filter($var) {
if($var["EventType"] == "UN" && $var["LocationURL"] == $this->univ_seg)
return true;
else
return false;
}
}
$data = json_encode(Array(
Array("StartDate"=>"2014/07/31","LocZipCode"=>"19406","LocationURL"=>"FSU","EventType"=>"UN"),
Array("StartDate"=>"2014/08/31","LocZipCode"=>"23513","LocationURL"=>"FSU","EventType"=>"UN"),
Array("StartDate"=>"2014/11/30","LocZipCode"=>"92108","LocationURL"=>"BU","EventType"=>"UN"),
Array("StartDate"=>"2014/09/30","LocZipCode"=>"78661","LocationURL"=>"BU","EventType"=>"UN")
));
$univ_seg = "BU";
getUA($data,$univ_seg);
function getUA($data, $univ_seg) {
$f = new univFilter();
$f->univ_seg = $univ_seg;
$univ_sched = json_decode($data, true);
$data = array_filter($univ_sched,array($f,"filter"));
print_r($data);
}
is it possible if callback in array_filter receive parameter?
You can create a closure on PHP ≥5.3.
array_filter($files, function($array) use ($key) {
return is_inarr_key($array, $key);
} );
If you are stuck with PHP <5.3, …
You can make $key
a global variable.
function is_inarr_with_global_key($array) {
global $key;
....
}
You can create a class
class KeyFilter {
function KeyFilter($key) { $this->key = $key; }
function is_inarr_key($array) { ... }
}
...
array_filter($files, array(new KeyFilter('name'), 'is_inarr_key'));
You can create 3 different functions
array_filter($files, 'is_inarr_name');
array_filter($files, 'is_inarr_path');
array_filter($files, 'is_inarr_number');
You can write your own array_filter
function my_array_filter($files, $key) {
...
}
Use external variable in array_filter
The variable $id
isn't in the scope of the function. You need to use the use
clause to make external variables accessible:
$foo = array_filter($bar, function($obj) use ($id) {
if (isset($obj->foo)) {
var_dump($id);
if ($obj->foo == $id) return true;
}
return false;
});
What is the default value for callback argument of array_filter function
If the source code of array_filter contains a second parameter, it is checked whether it is a real callable function. A string can also be passed. But even then it is checked whether this is a real function.
Variant 1: Define a function, then use its name
function isNotEmpty($val){
return !empty($val);
}
$arr = [0,1,[],null,false,"a","",(object)[],-1];
$res = array_filter( $arr);
$res1 = array_filter( $arr, 'isNotEmpty');
var_dump($res === $res1); //bool(true)
Variant 2: Use the boolval function
$arr = [0,1,[],null,false,"a","",(object)[],-1];
$res = array_filter( $arr);
$res1 = array_filter( $arr, 'boolval');
var_dump($res === $res1); //bool(true)
With the function boolvar () this also works apparently. However, the PHP manual says:
var The scalar value being converted to a bool.
But the examples for the boolval function also show an empty array as a value.
Correct syntax for php array_filter?
Seems like missing use
keyword. Try this:
$all_together = array_filter($info, function($each_one) use ($extra) {
$op = $each_one["something"];
if($op <= $extra) return $each_one["what_I_need"];
});
Related Topics
Access PHP Variable in JavaScript
PHP Decoding and Encoding JSON with Unicode Characters
Angularjs Upload Multiple Files with Formdata API
JSON_Encode Function: Special Characters
Check If Download Is Completed
Rotate Array Elements to the Left (Move First Element to Last and Re-Index)
Get Date Range Between Two Dates Excluding Weekends
Best Way to Get Files from a Dir Filtered by Certain Extension in PHP
Send Cookie with File_Get_Contents
How to Make Number_Format() Not to Round Numbers Up
How to Find Unused Functions in a PHP Project
Setting Environment Variables for Accessing in PHP When Using Apache
Package 'Php5-Gd' Has No Installation Candidate