Join the Stack Overflow Community
Stack Overflow is a community of 6.4 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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

share|improve this question
    
What do you want $item2 to be? A constant value? – knittl Jan 5 '12 at 15:58
1  
I think your call to array_map is flawed. Could you provide a proper example? – Felix Kling Jan 5 '12 at 16:01
    
array_map : The number of parameters that the callback function accepts should match the number of arrays passed to the array_map() Trick : You can use a array as param holder e.g. array array_map ( callable $callback, array_to_map,array('param') ) – ravisoni May 7 '14 at 9:01
up vote 4 down vote accepted

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.

share|improve this answer

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" }

share|improve this answer
    
It doesn't seem to work the way you described. Test it. The third parameter needs to be an array of elements - each of these elements is passed as an argument together with the matching element from the array being trimmed: $trimmed = array_map('trim',array('/some/','/link'),array('/', '/')); – tmt Jul 28 '14 at 10:18
    
@cascaval I had corrected on my own end after noticing the same and forgot to come back to post update. Thanks for the heads up, please see the updated answer, and if useful, please re-vote ;) – oucil Jul 28 '14 at 10:51
1  
Now it works well. +1 – tmt Jul 28 '14 at 10:57

Consider using array_walk. It allows you to pass user_data.

share|improve this answer
    
But how can we pass parameters if more than one argument? – Apul Gupta Oct 30 '14 at 14:52

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'); }
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.