Array_map功能与参数PHP [英] Array_map function in php with parameter

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

问题描述

我有这种

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

这将允许您创建一个自定义映射器,可以返回任何类型的信息......你可以complexify你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.

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

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