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
0
votes
1 answer

PHP Standard Deviation. Array_map function not found inside class function

I'm trying to perform a Standard Deviation with PHP. I used this function I founded there because the stats_standard_deviation doesn't seem to be reliable
Wistar
  • 3,770
  • 4
  • 45
  • 70
0
votes
1 answer

array_map triple dimensional array

How do I run a array_map on a triple dimensional array? Where I want to "clear" the innermost array? It looks like this: Array ( [1] => Array ( [1] => Array ( [cat] => Hello! …
JohnSmith
  • 417
  • 4
  • 10
  • 21
0
votes
3 answers

Ignore duplicate values in PHP - MYSQL using Arraymap

DB Date Price qty 2012-10-21 $11 150 2012-10-22 $12 90 2012-10-22 $12 10 2012-10-23 $13 250 CODE $result = mysqli_query($con, "SELECT * FROM table ORDER BY 'date'"); while ($row = mysqli_fetch_array($result)) { $orders[] =…
Amal Prasad
  • 157
  • 1
  • 4
  • 14
0
votes
1 answer

Merge these array values into a new array?

I have the following array stored in $data Array ( [name] => JimBob [data] => Array ( [0] => Array ( [date] => 2013-15-5 [pole] => 2 [race] =>…
Anagio
  • 3,005
  • 9
  • 44
  • 81
0
votes
4 answers

Looking for array_map equivalent to work on keys in associative arrays

Suppose I have an associative array: $array = array( "key1" => "value", "key2" => "value2"); And I wanted to make the keys all uppercase. How would I do than in a generalized way (meaning I could apply a user defined function to…
RedBullet
  • 501
  • 6
  • 18
0
votes
2 answers

php array_map removes more than required

I have an array like the following . . . Array ( [code] => BILL [assets] => Array ( [en] => Array ( [labels] => Array ( [datestamp]…
jimlongo
  • 365
  • 2
  • 5
  • 21
0
votes
2 answers

PHP: Create array of arrays, ignoring empty arrays

I need to create an array of arrays. I have been using array_map(null,$a,$b,$c) to do this and it works fine, however, it doesn't work if one of the mapped arrays doesn't exist. To get around this problem I have used: $myArray= array(); if…
ticallian
  • 1,631
  • 7
  • 24
  • 34
0
votes
1 answer

Combine 2 associative arrays

I have 2 arrays that i have created from 2 different systems: URL changed I need to loop through the array on the left and find a match for the business, address and zip. If there is a match on the right side then I need to grab the id and add it to…
JD Vangsness
  • 697
  • 3
  • 10
  • 27
0
votes
2 answers

Sum two array rows in PHP on a single line

I remember once I came across some website where sum of 2 arrays items was performed on a single line using array_sum and array_map functions. Does anyone know how to do…
user965748
  • 2,227
  • 4
  • 22
  • 30
0
votes
1 answer

How to call array_map() without using a closure

I have something like this: public static function lengtOfElements($array){ return array_map(function($item){return strlen($item);},$array); } What I want to do is to use strlen($string) directly in array_map, but when I try it it won't work..…
Koray Tugay
  • 22,894
  • 45
  • 188
  • 319
0
votes
1 answer

array map + class method/function as callback + call_user_func

I don't know what I have done wrong with the code below but it is not working. if($_data_count = count($_csv_raw_array) > 0){ foreach($_csv_raw_array as $_csv_row){ array_push($this->_data,…
bn00d
  • 1,126
  • 3
  • 15
  • 21
0
votes
2 answers

PHP How to convert each element of array to timestamp?

I have a two-element array - $time echo var_dump($time) : array 0 => array 'otw' => string '12:00' (length=5) 'zam' => string '15:00' (length=5) 1 => array 'otw' => string '16:00' (length=5) 'zam' => string…
bilasek
  • 77
  • 2
  • 4
0
votes
2 answers

Using a callback function of a different class in array_map --PHP

Possible Duplicate: setting scope of array_map php I have a function called cube1() in a class called customExceptions. In another PHP script I need to use array_map(), and for the callback function I want to use the cube1() function in the…
Les_Salantes
  • 317
  • 6
  • 20
0
votes
1 answer

PHP arraym map and method of item class

I have array: array( 0 => new SomeClass(1), 1 => new SomeClass(2), 2 => new SomeClass(3), ) How can I use array map to call method (non-static) of SomeClass class for each item in my array?
Mirgorod
  • 31,413
  • 18
  • 51
  • 63
-1
votes
4 answers

How to properly map or reduce an Array of Arrays of objects in javascript?

I have the following array of array of objects that is a result of a CloudWatch query using AWS-SDK-V3 JS: const respCW = [ [ { field: '@timestamp', value: '2022-10-25 15:30:36.685' }, { field: '@message', value: 'foo' }, …
IgorAlves
  • 5,086
  • 10
  • 52
  • 83