php 带有参数的php中的Array_map函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8745447/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Array_map function in php with parameter
提问by user1029834
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
有人可以帮助我吗?我尝试了很多东西,但没有任何效果
采纳答案by Mathieu Dumoulin
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.
这将允许您创建一个可以返回任何类型信息的自定义映射器......并且您可以使您的 customMapper 复杂化以轻松接受更多字段或配置。
回答by Serge S.
You can use an anonymous functionand transmit value of local variable into your myarraymap
second argument this way:
您可以使用匿名函数并通过myarraymap
这种方式将局部变量的值传输到您的第二个参数中:
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'); }
回答by oucil
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:
PHParray_map
支持第三个参数,它是一个数组,表示传递给回调函数的参数。例如,/
可以像这样从所有数组元素中修剪字符:
$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.
比使用自定义函数或其他函数(如array_walk
等)容易得多。
*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()
.
*NB 正如在下面的评论中指出的那样,我有点仓促,第三个参数确实需要与使用array_fill()
.
The above outputs:
以上输出:
array(2) {
[0]=>
string(4) "some"
[1]=>
string(4) "link"
}
回答by anubhava
Consider using array_walk. It allows you to pass user_data
.
考虑使用array_walk。它允许您通过user_data
。
回答by Renan Coelho
Consider using the third parameter from array_map
function to pass additional parameter to callable
function. Example:
考虑使用array_map
函数的第三个参数将附加参数传递给callable
函数。例子:
<?php
function myFunc($param) {
echo $param;
}
$array = ['foo', 'bar'];
array_map('myFunc', $array, ['the param']);
// the param
// the param
Ref: https://www.php.net/manual/en/function.array-map.php
参考: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."
文档提到了array_map
函数中的第三个参数:“通过回调函数运行的数组参数的补充变量列表。”