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
310
votes
18 answers

Access first level keys with array_map() without calling `array_keys()`

Is there a way of doing something like this: $test_array = array( "first_key" => "first_value", "second_key" => "second_value" ); var_dump( array_map( function($a, $b) { return "$a loves $b"; }, …
José Tomás Tocino
  • 9,873
  • 5
  • 44
  • 78
170
votes
5 answers

Performance of foreach, array_map with lambda and array_map with static function

What's the performance difference (if there is any) between these three approaches, both used to transform an array to another array? Using foreach Using array_map with lambda/closure function Using array_map with 'static' function/method Is there…
Pavel S.
  • 11,892
  • 18
  • 75
  • 113
84
votes
4 answers

Can a method be used as an array_map function

I want to do something like this: class Cls { function fun($php) { return 'The rain in Spain.'; } } $ar = array(1,2,3); $instance = new Cls(); print_r(array_map('$instance->fun', $ar)); // ^ this won't work but the first…
allyourcode
  • 21,871
  • 18
  • 78
  • 106
64
votes
4 answers

array_map inline anonymous function

I tested inline anonymous function with array_map here and it worked but when I tried same with $user_meta it is not working. $user_meta = Array ( [interest] => Array ( [0] => Array ) [type] => Array ( [0] => Array ) [user_status] => Array (…
B L Praveen
  • 1,812
  • 4
  • 35
  • 60
62
votes
9 answers

How to implement class methods in array_map's callable

I am trying to create a class to handle arrays but I can't seem to get array_map() to work in it. $array = [1,2,3,4,5,6,7,8,9,10]; class test { public $values; public function adding($data) { $this->values = array_map($this->dash(),…
Justin
  • 1,249
  • 1
  • 15
  • 37
61
votes
1 answer

PHP error. Why is "variable undefined" inside array_map?

I am using array_map function in my php application. I defined the array_map function like this. $ratingID = $this->db->insert_id(); $rated_item_array = array_map(function ($a) { return $a + array('RatingID' => $ratingID); },…
Kanishka Panamaldeniya
  • 17,302
  • 31
  • 123
  • 193
52
votes
3 answers

Select/map each item of a Powershell array to a new array

I have an array of file names in Powershell, and I would like to prepend a path to each of them and get the result in a new array. In C# I could do this using Linq... var files = new string[] { "file1.txt", "file2.txt" }; var path = @"c:\temp\"; var…
Jon Rimmer
  • 12,064
  • 2
  • 19
  • 19
39
votes
1 answer

Call methods of objects in array using array_map?

Suppose I have simple class like: class MyClass { private $_prop; public function getProp() {return $this->_prop;} [....] } Now what I want to do somwhere not in scope of MyClass is to get array of $_prop from array of objects of…
morphles
  • 2,424
  • 1
  • 24
  • 26
36
votes
2 answers

array_map vs loop and operation

Using: for($i=1; $i<= 10000; ++$i) { $arrayOfNumbers[] = rand(1, 99999); } Can some explain why there is such a speed difference: array_map(array($maxHeap, 'insert'), $arrayOfNumbers); # Avg Time: 0.92856907844543s #…
Lizard
  • 43,732
  • 39
  • 106
  • 167
31
votes
5 answers

array_map on collection with array interfaces?

I have a class called Collection which stores objects of same type. Collection implements array interfaces: Iterator, ArrayAccess, SeekableIterator, and Countable. I'd like to pass a Collection object as the array argument to the array_map function.…
f1ames
  • 1,714
  • 1
  • 19
  • 36
30
votes
4 answers

php array_map with static method of object

I want to use array_map with a static method but I fail. Here is my code : Class Buy { public function payAllBills() { $bill_list = OtherClass::getBillList(); return array_map(array(self, 'pay'), $bill_list); // Issue line …
Samuel Dauzon
  • 10,744
  • 13
  • 61
  • 94
26
votes
4 answers

array_map with str_replace

Is it possible to use array_map in conjunction with str_replace without calling another function to do the str_replace? For example: array_map(str_replace(' ', '-', XXXXX), $myArr);
Lizard
  • 43,732
  • 39
  • 106
  • 167
24
votes
10 answers

What is the concept of Array.map?

I am having problems understanding the concept of Array.map. I did go to Mozilla and Tutorials Point, but they provided very limited info regarding this. This is how I am using Array.map. It is a little complex (a bit of d3.js involved; just ignore…
user2412575
19
votes
6 answers

how to return nothing ( empty array) with array.map function

Right now, if 'Everything' in the list is detected, the output becomes [""]. Expected output: [] Copy.names = rule.names.map(function(x) { if (x.name ==='Everything') { …
Angular
  • 603
  • 2
  • 9
  • 24
19
votes
4 answers

How to convert an array of arrays or objects to an associative array?

I'm used to perl's map() function where the callback can assign both the key and the value, thus creating an associative array where the input was a flat array. I'm aware of array_fill_keys() which can be useful if all you want to do is create a…
Tom Auger
  • 19,421
  • 22
  • 81
  • 104
1
2 3
29 30