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

php - array map using public function callback within class

class something{ public function add_val( $val ){ $array = array(); foreach( $val as $value ) { $array[] = static::$post[${$value}]; } return $array; } pulblic function somethingelse(){ .... …
Phil Jackson
  • 10,238
  • 23
  • 96
  • 130
4
votes
1 answer

Button Click in array map using react js

I was going through couple of examples from official website. I found below example which sets the button values based on click. example With reference to above example, I used array map and tried to set button value of perticular button. class…
user3335796
  • 1,227
  • 1
  • 17
  • 24
4
votes
1 answer

How to use str_pad using array_map in PHP?

I'm trying to use str_pad using array_map in PHP. But it seems like it is not working. A code-snippet: $arr = ["", "a", "ab", "abc", "abcd"]; $arr = array_map("str_pad", $arr, [4, "0", STR_PAD_LEFT]); var_dump($arr); Result is: array(5) { [0]=> …
Guy
  • 91
  • 7
4
votes
2 answers

Best way to call function on every object in array? Without for loop?

I'm curious if there's a better way to do what I'm doing. I'm fairly new to php so I'm interested in what others think who have spent more time with the language. What I want to do: call a function on every object in an array What I'm doing:…
Kristofer Doman
  • 439
  • 1
  • 7
  • 12
4
votes
1 answer

How return is working from array_map PHP

I'm fairly new to PHP, but I understand once PHP hits a return in a function, it will exit out of the function and return to where it was called. I am confused to how or why in the function below array_map() starts with return and has another inside…
assertcarl
  • 77
  • 1
  • 10
4
votes
1 answer

Using range operator with a step

The PowerShell range operator generates a list of values: >1..6 1 2 3 4 5 6 How can I generate a list of values with a specific step? For example, I need a list from 1 to 10 with step 2.
constructor
  • 1,412
  • 1
  • 17
  • 34
4
votes
2 answers

Memory leak?! Is Garbage Collector doing right when using 'create_function' within 'array_map'?

I found following solution here on StackOverflow to get an array of a specific object property from array of objects: PHP - Extracting a property from an array of objects The proposed solution is to use array_map and within create a function with…
algorhythm
  • 8,530
  • 3
  • 35
  • 47
4
votes
2 answers

Call class method from inside array_map anonymous function

I am trying to call one of my object's methods from within an array_map anonymous function. So far I am receiving the expected error of: Fatal error: Using $this when not in object context in... I know why I am getting this error, I just don't…
Ben Carey
  • 16,540
  • 19
  • 87
  • 169
3
votes
3 answers

jquery map convert list of dashed properties into object

I would like to convert something like this (used in eg. in class): var classname = "username_admin color_red strength_good" into: myvalues = { username: 'admin', color: 'red', strength: 'good' } Here's at present my closest…
Adit Saxena
  • 1,617
  • 15
  • 25
3
votes
0 answers

Array Map with Custom Types

I have the following types: export type Point = [number, number]; export type LineType = [Point, Point]; export const hexPoint = (x: number, y: number, p: number): Point => { const [px, py] = coordinateToPoint(x, y); return [ px +…
Djave
  • 8,595
  • 8
  • 70
  • 124
3
votes
1 answer

REACT JS: How to add a Key to an array that hasn't been initiated, inside a map?

I been trying for a few days to solve this piece of code and have successfully slowly progress through it but I'm still stuck sadly here's the issue: First of all what I want to achieve is to display an Array of X with all the values starting on 1…
ReactPotato
  • 1,262
  • 3
  • 26
  • 46
3
votes
1 answer

How to show two columns in each row is Scrollview not Flatlist? React Native

I need to show an array and I need to render it using array.map not Flatlist because of the orientation issue. In Flatlist, I can do it easily using numColumns = {2} but I really need to do that using array.map, not Flatlist, I can render the array…
user16350114
3
votes
2 answers

Typescript array.map doesn't distribute intersection types properly

I have an array of type object[] & Tree[] but arr.map(child => ...) infers the type of the child as object rather than object & Tree. Is there any way to avoid this without extra casting? Notably Tree extends object but typescript doesn't seem to…
Hugheth
  • 1,802
  • 17
  • 14
3
votes
1 answer

What if there is conflict of hashing of keys in ArrayMap or SparseArray?

I have read many blogs for array map and sparse array where every where I am getting that ArrayMap contains two small array instead of one in a HashMap. The first array (Hash-Array) contains the specified hash keys in sorted order. The second…
Anand Kumar Jha
  • 614
  • 9
  • 23
3
votes
1 answer

How to take array values within json data in react native?

Hello I'm trying to render a fetch response from server like the following .then(responseData => { responseData.map(detail => { let currIndex = -1; let k = detail.data.curriculum.reduce((acc,…
Linu Sherin
  • 1,712
  • 7
  • 38
  • 88