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

Looking for function similar to array_map but which the same arg each time to the callback

As someone who is learning PHP I was experimenting with the arrap_map function. I was hoping that it would pass the same 3rd arg each time through to the called function. As below, this is not the behaviour of array_map. Is there an alternative…
weaveoftheride
  • 4,092
  • 8
  • 35
  • 53
-1
votes
1 answer

array_map and mysqli_real_escape_string

i know that on stack, exists a lot of questions related to this.. but i didnt found my answer Im trying to create a function that returns an array of POST values. and i'm getting error on an array map: function clean_the_data ($data) { if (…
-1
votes
1 answer

How to rewrite this PHP code that uses array_map?

I have this line of code : $roomservicesids = array_map(function($v){ return $v['serviceID'];}, $roomsservices); It works great on a server where I have PHP > 5.3 On another server, where I have PHP < 5.3, it doesn't work. I am trying to rewrite…
user2417624
  • 653
  • 10
  • 32
-1
votes
4 answers

Effective and elegant way to get an item from array

For example, i've got an array like this: $a = array( 0 => array( 'foo' => 42 ), 1 => array( 'foo' => 143 ), 2 => array( 'foo' => 4 ) ); And i need to get an element with a maximum value in 'foo'.…
UnstableFractal
  • 1,403
  • 2
  • 15
  • 29
-2
votes
1 answer

Why the extra implode, array_map, etc functions instead of returning simply a string from the start? Trying to move this PHP to node

private static function getOrdering($sortingColumn, $sortingDirection) { if ($sortingColumn === 'reportTime') { return implode(', ', array_map(function ($column) use ($sortingDirection) { return $column .…
kastaplastislife
  • 293
  • 2
  • 16
-2
votes
1 answer

How to use javascript map to combine numbers at every nth element?

I like to combine numbers at every 4th index of an array. In the following oversimplified example, I did using "for" loop. Instead of that, I like to learn how to use "map" to achieve the same result. Thanks for any help! function test() { var…
Newbie
  • 247
  • 3
  • 11
-2
votes
4 answers

How could I get the output like objects of array within a key of another objects of array

so I have an Arrray of objects : [ { MenuId: 'GM002', MenuName: 'Profile', MenuImage: 'CgProfile', Orderno: '2', SubMenuId: 'SM001', SubMenuName: 'Personal Information', SubMenuImage: 'BsPerson', SubMenuOrderno:…
-2
votes
1 answer

Mapping over elements matching string

I have a map .map((item: { amount: number}) => The issue is that object prop is not always amount but will start with that. so it could be .map((item: { amountTotal: number}) => .map((item: { amountConsolidated: number}) => Is there a way to regex…
TommyD
  • 913
  • 3
  • 17
  • 32
-2
votes
1 answer

Further understanding of array.map

I'm currently learning JS and I'm not quite sure what this does. Could I please get some explanation? num = num.map(x => { return (x == 0) ? 1: 0; }).join(""); Thank you.
-2
votes
1 answer

Javascript map method not working as expected

What cause printing every array index twice time using map method. Does component is double rendered? Why it working like that? import React from 'react'; const data = [ { id: 1, name: 'john' }, { id: 2, name: 'peter' }, { id: 3, name:…
Telirw
  • 373
  • 3
  • 15
-2
votes
2 answers

Create a new array from a multidimensional array by those arrays have same length in php

I've a multidimensional array looks like below- Array ( [0] => Array ( [0] => Account Number [1] => Account Title [2] => Account Type [3] => Currency [4] => Available Balance …
-2
votes
1 answer

How to pass raw HTML string through array_map in php ignoring HTML tags

For the below program, I want to output the strings as they are in $a, but in my case they are being converted to html format. How can I bypass the conversion.
Frosted Cupcake
  • 1,909
  • 2
  • 20
  • 42
-2
votes
1 answer

How To get sub array keys from array by less than values

I want to get sub array keys from array by less than values. This is an example: $arr_less_than = array(55,60,10,70); $BlackList = array(10,8,15,20); $MasterArray = array( 10 => array(1 => array(50,20,5,40), 2 => array(70,77,58,10), 3 =>…
samer
  • 1
  • 4
-2
votes
1 answer

Array map, flatten, and unset on a single multidimensional array

I've got a multidimensional array of the format: Array ( [0] => Array ( [course_prefix] => AERO [0] => AERO [course_number] => 101 [1] => 101 ) [1] => Array ( [course_prefix] => AERO [0]…
Marcatectura
  • 1,721
  • 5
  • 30
  • 49
-2
votes
2 answers

How to apply a function with multiple arguments to every element of an array in PHP?

I have a method MyClass#foo(array $array, $argX, $argY, $argZ) and want to apply it to every element of $myArray. I've tried this with array_map(...) and array_walk(...), but they are not flexible enough for a function with a custom signature. How…
automatix
  • 14,018
  • 26
  • 105
  • 230
1 2 3
29
30