'Array_map function in php with parameter
I have this
$ids = array_map(array($this, 'myarraymap'), $data['student_teacher']);
function myarraymap($item) {
return $item['user_id'];
}
I will would like to put an other parameter to my function to get something like
function myarraymap($item,$item2) {
return $item['$item2'];
}
Can someone can help me ? I tried lots of things but nothing work
Solution 1:[1]
Apart from creating a mapper object, there isn't much you can do. For example:
class customMapper {
private $customMap = NULL;
public function __construct($customMap){
$this->customMap = $customMap;
}
public function map($data){
return $data[$this->customMap];
}
}
And then inside your function, instead of creating your own mapper, use the new class:
$ids = array_map(array(new customMapper('param2'), 'map'), $data['student_teacher']);
This will allow you to create a custom mapper that can return any kind of information... And you can complexify your customMapper to accept more fields or configuration easily.
Solution 2:[2]
You can use an anonymous function and transmit value of local variable into your myarraymap second argument this way:
function myarraymap($item,$item2) {
return $item[$item2];
}
$param = 'some_value';
$ids = array_map(
function($item) use ($param) { return myarraymap($item, $param); },
$data['student_teacher']
);
Normally it may be enough to just pass value inside anonymous function body:
function($item) { return myarraymap($item, 'some_value'); }
As of PHP 7.4, you can use arrow functions (which are basically short anonymous functions with a briefer syntax) for more succinct code:
$ids = array_map(
fn($item) => myarraymap($item, $param),
$data['student_teacher']
);
Solution 3:[3]
PHP's array_map() supports a third parameter which is an array representing the parameters to pass to the callback function. For example trimming the / char from all array elements can be done like so:
$to_trim = array('/some/','/link');
$trimmed = array_map('trim',$to_trim,array_fill(0,count($to_trim),'/'));
Much easier than using custom functions, or other functions like array_walk(), etc.
N.B. As pointed out in the comments below, I was a little hasty and the third param does indeed need to be same length as the second which is accomplished with array_fill().
The above outputs:
array(2) {
[0]=>
string(4) "some"
[1]=>
string(4) "link"
}
Solution 4:[4]
Consider using array_walk. It allows you to pass user_data.
Solution 5:[5]
Consider using the third parameter from array_map function to pass additional parameter to callable function. Example:
<?php
function myFunc($item, $param) {
echo $param . PHP_EOL;
}
$array = ['foo', 'bar'];
array_map('myFunc', $array, ['the foo param', 'the bar param']);
// the foo param
// the bar param
Ref: https://www.php.net/manual/en/function.array-map.php
The docs said about the third param from array_map function: "Supplementary variable list of array arguments to run through the callback function."
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Mathieu Dumoulin |
| Solution 2 | outis |
| Solution 3 | Gleb Kemarsky |
| Solution 4 | anubhava |
| Solution 5 |
