I got this multiple array named $files[], which consists of keys and values as below:
[
[
'name' => 'index1.php',
'path' => 'http://localhost/php/gettingstarted/',
'number' => 1,
],
[
'name' => 'index10.php',
'path' => 'http://localhost/php/gettingstarted/',
'number' => 2,
],
[
'name' => 'index11.php',
'path' => 'http://localhost/php/gettingstarted/',
'number' => 3,
],
]
I use this code to create a new array consist of ‘name’ keys only. But it failed
array_filter($files, "is_inarr_key('name')");
function is_inarr_key($array, $key)
{
//TODO : remove every array except those who got the same $key
}
and I get this error:
array_filter() [function.array-filter]: The second argument, ‘is_inarr_key(‘name’)’, should be a valid callback in C:\xampp\htdocs\php\gettingstarted\index.php on line 15
So my questions are:
- Is it possible to make the call-back function on
array_filterreceive parameter? - What is the general rule of thumb on how to use callback in any PHP built-in function?
You can create a closure on PHP ≥5.3.
If you are stuck with PHP <5.3, …
You can make
$keya global variable.You can create a class
You can create 3 different functions
You can write your own
array_filter