Questions tagged [array-map]

An array map function creates a new array by calling a callback function for each element of the provided input array. PHP: array_map( $callback, $input_array ), JavaScript: inputArray.map( callback ).

PHP's array_map() function accepts a callback function to run for each element in each array and an array to run through the callback function. It returns an array containing all the elements of arr1 after applying the callback function to each one.

array array_map ( callable $callback , array $array1 [, array $... ] )

array_map() — Applies the callback to the elements of the given arrays

Example:

function cube($n)
{
    return($n * $n * $n);
}
$a = array(1, 2, 3, 4, 5);
$b = array_map("cube", $a);
print_r($b);

Output:

Array
(
    [0] => 1
    [1] => 8
    [2] => 27
    [3] => 64
    [4] => 125
)
445 questions
18
votes
2 answers

What is the difference between the hash-map and array-map in clojure?

Clojure has an array-map and hash-map, I couldn't figure out the difference between the two. Could anyone explain if possible with an example as to when either of them would be used?
Harsh Shah
  • 368
  • 3
  • 17
18
votes
2 answers

How can i pass a single additional argument to array_map callback in PHP?

How can i pass a single additional argument to array_map callback? In my example i'd like to pass $smsPattern (as a second argument, after current element in $featureNames) to the function array_map with $getLimit closure: $features =…
gremo
  • 47,186
  • 75
  • 257
  • 421
16
votes
3 answers

Why won't trim work as a callback for array_walk or array_map in PHP?

Why does my sample code result in the first string still having a trailing space? $a=array('test_data_1 ','test_data_2'); array_walk($a, 'trim'); array_map('trim', $a); foreach($a AS $b){ var_dump($b); } string(12)…
Jaak Kütt
  • 2,566
  • 4
  • 31
  • 39
15
votes
6 answers

How can I use array_map with keys and values, but return an array with the same indexes (not int)?

I have an array such as ['id' => 1, 'name' => 'Fred']. I want to call array_map on this array and also use the key inside the function. However, when I make a return, my keys will become int. Simple example : $arr = array('id' => 1, 'name' =>…
user2816456
  • 569
  • 2
  • 5
  • 15
13
votes
3 answers

Array_Map using multiple native callbacks?

I want to run 3 native functions on the same array: trim, strtoupper and mysql_real_escape_string. Can this be done? Trying to pass an array as a callback like this isn't working: $exclude =…
J. Scott Elblein
  • 4,013
  • 15
  • 58
  • 94
10
votes
3 answers

Javascript seperating array of objects into multiple arrays using map(), and Iterator

Basically I have an array of objects like var array = [{x: 10, y: 5, r: 10, ...} ] There are more fields in the object, but to the point, this is the way I've found to seperate the fields to arrays: var x = array.map(obj => obj.x); var y =…
user3599803
  • 6,435
  • 17
  • 69
  • 130
10
votes
2 answers

Multiple functions using array_map

array_map accepts string as its first argument. Is there a way, to use arrays instead of strings, like: .... array_map( array('trim','urlencode'), $my_array); so I could attach multiple functions.
T.Todua
  • 53,146
  • 19
  • 236
  • 237
9
votes
4 answers

Skip iteration of array_map function if IF statement True

Is there a way to break or continue an iteration of the built-in method array_map() as you would in a normal for loop? For example: array_map(function (String s) { if (condition is met){ continue; } return stuff; },…
d1596
  • 1,187
  • 3
  • 11
  • 25
9
votes
1 answer

Why does the function signature differ between array_map and array_filter/array_reduce?

array_map asks for the $array input as the last parameter(s). array_filter and array_reduce take $array input as the first parameter. As a contrasting example, when you call map, filter or reduce on an array in JavaScript, the callback function…
trad
  • 7,581
  • 4
  • 20
  • 17
9
votes
2 answers

Why does array_map() with null as callback create an "array of arrays"?

Today I learned about a special case of array_map() in PHP, which is mentioned as a side note in the documentation: Example #4 Creating an array of arrays
Fabian Schmengler
  • 24,155
  • 9
  • 79
  • 111
9
votes
3 answers

Get array values based on bitmask in PHP

I have a 32-bit integer used for bitmask and an array with 32 values. How to get only those values from the array which indexes correspond to the positions of the non-zero bits in the bitmask? For example let say that the bitmask is 49152 which is…
Nikola Obreshkov
  • 1,698
  • 4
  • 21
  • 32
8
votes
2 answers

Alternatives to Pass both Key and Value By Reference:

Can someone explain to me why you can't pass a key as reference? Ex: if(is_array($where)){ foreach($where as &$key => &$value){ $key = sec($key); $value = sec($value); } unset($key, $value); } Throws: Fatal error: Key…
Ben
  • 745
  • 7
  • 23
7
votes
0 answers

Having trouble passing named arguments to internal functions with variadic parameters

I played a bit with php's array_map function and thought about using it with named arguments, as the parameter order always irritates me. (The functions don't do something useful. Just needed something to test.) So far so good, this is working as…
Sindhara
  • 1,423
  • 13
  • 20
7
votes
2 answers

trim() function : How to avoid empty string return if the argument is unset/null variable?

I have a problem when using the trim() function in php. //Suppose the input variable is null. $input = NULL; echo (trim($input)); As shown above, the output of the codes is empty string if the input argument if NULL. Is there any way to avoid…
easycoder
  • 305
  • 2
  • 3
  • 12
7
votes
2 answers

PHP array_map callback function inside function

I have a function called createCost, and inside that function, I have an array_map that takes in an array and a function called checkDescription that's inside that createCost. Below is an example: public function createCost{ $cost_example =…
Greg Sithole
  • 145
  • 1
  • 2
  • 11
1
2
3
29 30