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
7
votes
3 answers

Array Map a PHP Multidimensional Array

Have a multidimensional array $template=Array ( [0] => Array ( [id] => 352 [name] => a ) [1] => Array ( [id] => 438 [name] => b ) [2] => Array ( [id] => 351 [name] => c ) ) and an array map function function…
random_geek
  • 345
  • 2
  • 8
  • 23
7
votes
2 answers

Crash with Android 4.1 with ArrayMap

I get an error in my code with this logcat: java.lang.NoClassDefFoundError: android.util.ArrayMap at it.dd.multiplayerit.MainActivity.(MainActivity.java:88) at…
Atlas91
  • 5,754
  • 17
  • 69
  • 141
7
votes
1 answer

How to get a clojure array-map to maintain insertion order after assoc?

I have an array-map which I am associng some values into it. After a certain size the returned value is a PersistentHashMap rather than the original PersistentArrayMap. I've read about this behavior on a few web sites. Is there any way to force…
Sonicsmooth
  • 2,673
  • 2
  • 22
  • 35
6
votes
3 answers

array_map and append string to elements of an array

I have an array like this: $a = array('aa', 'bb', 'cc', 'dd'); I want to add the 'rq' string at the beginning of all the elements of the array. Is it possible to do it by calling array_map() on this array?
hd.
  • 17,596
  • 46
  • 115
  • 165
6
votes
3 answers

Why does memory overflow when using array_map for big array?

When I'm testing array_map() function. There is a very strange phenomenon. Normal size array $array = range(1, 100000); echo memory_get_usage(); array_map(function($value) {}, $array); echo memory_get_usage(); Result 8649024 8649024 It's obvious…
Tom Tao
  • 97
  • 7
6
votes
5 answers

How can I update a json object inside of an array?

I have an array of objects and I want to update some of the content. I figured I could just map through the objects, find the match I was looking for and than update it. data = data.map(obj => { return this.state.objToFind === obj.title; …
user3622460
  • 1,231
  • 5
  • 23
  • 42
6
votes
4 answers

Can I set the keys of an array using array functions like array_map

I really like the functional programming style of using array map to create an array of objects from another array of objects. $newObjects = array_map( function($oldObject) { return new NewObject($oldObject); }, $oldObjects ); Which all…
6
votes
3 answers

Find min/max in a two dimensional array

I have an array with the following format: Array ( [0] => Array ( [DateTime] => "2013-05-22 14:21:01" [Price] => 102.01 ) [1] => Array ( [DateTime] => "2013-05-23 15:55:01" …
Chuck Le Butt
  • 47,570
  • 62
  • 203
  • 289
5
votes
2 answers

Is there a way to send parameters into a callback function without creating my own function first?

I have an array of values that I would like to run through htmlspecialchars but with an argument such as this: $param = htmlspecialchars($param, ENT_QUOTES); The problem is, I have an array of values that I want to run htmlspecialchars on: $array =…
Francis Lewis
  • 8,872
  • 9
  • 55
  • 65
5
votes
3 answers

PHP array_map trim + parameters

I'm using array_map to trim all my array values but I need to pass a third parameter because I need to more than just trim whitespaces so I'm passing in a third parameter. Basically I want to trim all array values of whitespaces, single quotes, and…
dokgu
  • 4,957
  • 3
  • 39
  • 77
4
votes
3 answers

Use array_map to return an array of instantiated objects?

Say I have the following: class Thing { function __construct($id) { // some functionality to look up the record and initialize the object. return $this; } } Now given an array of IDs, I want to end up with an array of instantiated…
robertwbradford
  • 6,181
  • 9
  • 36
  • 61
4
votes
1 answer

React: How to prevent re-rendering child components in `map`?

I tried to boil down the problem into an example as simple as possible: We have a list of child components, each called NumChoice, each representing a number. NumChoice is wrapped in React.memo. In the parent component, we have an array of booleans,…
1man
  • 5,216
  • 7
  • 42
  • 56
4
votes
5 answers

PHP - Array mapping or filtering

Sorry for the vague title, I had trouble summarizing this question in one sentence (I'm open to suggestions or edits). I have a 2 dimensional associative array with an array of 2 numbers for each key. Like this: Array ( [one] => Array ( …
vulpinus
  • 79
  • 1
  • 7
4
votes
1 answer

Passing props in map function doesn't work. Passing it as key works

I'm passing an array as props from the App.js to Component B. The array: myOptions: [ { id: 1, option: "Option1" }, { id: 2, option: "Option2" }, { id: 3, option: "Option3" } ] From the App.js I'm passing it to Component…
Somename
  • 3,376
  • 16
  • 42
  • 84
4
votes
2 answers

Pythonic way of getting a list of IDs from a dictionary

Assuming I have the following data structure theValues = [ { "id": "123", "name": "foo" }, { "id": "321", "name": "bar" }, { "id": "231", "name": "baz" } ] What's the best Pythonistic way to get a list of the IDs [123,321,231] ? If this were…
Armstrongest
  • 15,181
  • 13
  • 67
  • 106
1 2
3
29 30