array_map applied on a function with 2 parameters
Posted
by
mat
on Stack Overflow
See other posts from Stack Overflow
or by mat
Published on 2012-10-08T01:51:39Z
Indexed on
2012/10/08
3:37 UTC
Read the original article
Hit count: 256
I've 2 arrays ($numbers
and $letters
) and I want to create a new array based on a function that combines every $numbers
with every $letters
. The parameters of this function involes the value of both $numbers
and $letters
. (Note: $numbers and $letters doesn't have the same amount of values). I need something like this:
$numbers = array(1,2,3,4,5,6,...);
$letters = array('a','b','c','d','e',...);
function myFunction($x,$y){
// $output = some code that use $x and $y
return $output;
};
$array_1 = array( (myFunction($numbers[0],$letters[0])), (myFunction($numbers[0],$letters[1])), myFunction($numbers[0],$letters[2]), myFunction($numbers[0],$letters[3]), etc);
$array_2 = array( (myFunction($numbers[1],$letters[0])), (myFunction($numbers[1],$letters[1])), myFunction($numbers[1],$letters[2]), myFunction($numbers[1],$letters[3]), etc);
$array_3 = array( (myFunction($numbers[2],$letters[0])), (myFunction($numbers[2],$letters[1])), myFunction($numbers[2],$letters[2]), myFunction($numbers[2],$letters[3]), etc);
...
$array_N = array( (myFunction($numbers[N],$letters[0])), (myFunction($numbers[N],$letters[1])), myFunction($numbers[N],$letters[2]), myFunction($numbers[N],$letters[3]), etc);
$array = array($array_1, $array_2, $array_3, etc.);
I know that this may work, but it's a lot of code, especially if I have a many values for each array. Is there a way to get the same result with less code? I tried this, but it's not working:
$array = array_map("myFunction($value, $letters)",$numbers));
Any help would be appriciated!
© Stack Overflow or respective owner