带有参数的php中的Array_map函数 [英] Array_map function in php with parameter

查看:24
本文介绍了带有参数的php中的Array_map函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个

$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

推荐答案

除了创建映射器对象之外,您无能为力.例如:

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']);

这将允许您创建一个可以返回任何类型信息的自定义映射器......并且您可以使您的 customMapper 复杂化以轻松接受更多字段或配置.

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.

这篇关于带有参数的php中的Array_map函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆