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

How can we Recursively remap the Nested Array in Javascript?

Looking for recursively remap the nested array in TypeScript / Javascript. I have an array that has parent and children objects and wants to clone with a blank array with only has children and parentId object key. for Ex. [{ id: "abc", name:…
Amit
  • 27
  • 5
2
votes
2 answers

Using map without executing and combine bind - JavaScript

Could anyone please tell me why this const is a function? And how is this function used like a map? I was having a hard time understanding this code: const something = ['a','b','c'].map.bind([1,2,3]); Thank you 'Euan Smith' for explaining that…
Itay
  • 398
  • 2
  • 14
2
votes
3 answers

PHP - array_map 2 columns and remove duplicate rows by key

I have the following array dynamically generated from an external API: $purchases = array( array('product_id' => 7, 'receipt' => R13D13), array('product_id' => 5, 'receipt' => Y28Z14), array('product_id' => 7, 'receipt' => R02310), …
ianhman
  • 73
  • 1
  • 12
2
votes
3 answers

Trying to understand odd variant of a PHP array_map() call

I'm trying to understand some code I found the open source oauth-php library. The relevant code snippet is: protected function sql_printf ( $args ) { $sql = array_shift($args); if (count($args) == 1 && is_array($args[0])) { …
Robert Oschler
  • 14,153
  • 18
  • 94
  • 227
2
votes
0 answers

PHP array_map variable scope, how to pass variable to array_map function

I'm trying modify a CVS price list export file by passing a url variable defining the currency exchange rate. But if I try add variables to the array_map function, it says the variable are undefined, even though they are set higher up in the…
joshmoto
  • 4,472
  • 1
  • 26
  • 45
2
votes
1 answer

a better way to do this (probably using array_map)

I have this arrays : $a = array( 'key1' => array ( 'k1'=>'value1', 'k2'=>'value2' , 'k3'='' ), 'key2' => array ( 'k1'=>'value1', 'k2'=>'value2' , 'k3'='' ), ... ); and I have another array: $b = array('key1'=>'value…
fdias
  • 87
  • 5
2
votes
1 answer

Async request inside two Array.maps

I am creating an API to interface with my Google Cloud Firestore database. I am querying for 'pipelines' in my application. The request will return an array of pipelines (ie. an array of JSON objects). Each JSON object has a users attribute which is…
2
votes
5 answers

Javascript array restructuring for nested array

I have an array of objects as mentioned below. const inputArray =[ { name: "Energy", quantity: [ { qval: "100 ", unit: "unit1" }, { qval: "200 ", unit: "unit2" } ], }, { …
Shah
  • 79
  • 6
2
votes
1 answer

ES6 array filter to remove common items

I am trying to remove common occurrence of slot_id in two arrays and to build an array of objects with no duplicates of slot_id. Tried the following but its results an output array inside another array i.e. output [Array(0), Array(2)]. I am…
Dibish
  • 9,133
  • 22
  • 64
  • 106
2
votes
2 answers

Read external variable with create_function php

I need to append a prefix to each key in my array. The prefix it is defined outside the create_function I am using. How can I make it accessible from inside? Here my code ($result is my array of key => value): $groupName =…
user3563844
  • 113
  • 1
  • 8
2
votes
7 answers

Javascript how to join two arrays having same property value?

How do I join arrays with the same property value? I cannot map it because it has different indexes. var array1 = [ {'label':"label1",'position':0}, {'label':"label3",'position':2}, {'label':"label2",'position':1}, …
apelidoko
  • 782
  • 1
  • 7
  • 23
2
votes
2 answers

array_map - "Argument passed must be of the type array, string given" error

I am not sure why the following does not work. I get the above error (topic): $array = array (); //something goes in here function del_space(array $a){ foreach($a as $key => $value){ preg_replace("/; +/", "", $value); } } $no_space =…
user9731806
2
votes
0 answers

php pass by reference in array_map function

I am trying to use array_map function to parse query string and get a key=>value pair: function parse_query_string($string) { $result = []; $arry = explode('&',$string); $get_qry_str_array = function($str, &$result) { $a =…
Yeasir Arafat Majumder
  • 1,222
  • 2
  • 15
  • 34
2
votes
2 answers

How to synchronously nest subarrays from one array into subarrays of another?

I have 2 multi-dimensional arrays: $category = array ( 37 = array (id=1, name=joe, boss=kev) 73 = array (id=55, name=diane, boss=rox) 11 = array (id=4, name=bideo, boss=julia) ) $other_variable = array ( 1 = array ( …
ian
  • 303
  • 3
  • 15
2
votes
2 answers

Is there a way to have named indexes on array_map output?

I have the code below: array_map( function (ReflectionParameter $parameter){ $paramName = $parameter->getName(); return $this->params[$paramName]; , $this->getParametersNeededByTheConstructor() ); It outputs something…
Eleandro Duzentos
  • 1,370
  • 18
  • 36