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
2 answers

Handle multiple parameters in array_map call back

I have the following code: $a=array(15,12,13,25,27,36,18); $b=array(1,1,1,1,1,1,1);//is it possible to pass only one value=1, instead of array containing seven 1's // expectation: $b = array(1); or $b= 1; //instead of…
waqas
  • 91
  • 8
2
votes
1 answer

reactjs ,jsx iterate over nested object

i have an reactjs application which contains all child object, i want to iterate over them few things i have tried like cart.line_items.map(items =>)} and cart preview is like bellow import React from…
Anish
  • 558
  • 3
  • 10
  • 33
2
votes
1 answer

In Yii2 how to use array index as $key when using ArrayHelper::map() method?

$lang = [ 'en' => ['id'=>'1', 'name' => 'English', 'short' => '1', 'active' => '1',], 'tn' => ['id'=>'2', 'name' => 'Tamil', 'short' => '2', 'active' =>'1',], ]; // sample array` In yii2 i can use the array map method as…
Thiyaga Raj
  • 105
  • 3
  • 12
2
votes
1 answer

How to check if string matches pattern given?

Have a string like this: 'XXX-XXX-XXX'; This can be changed by the user. Than I have another string like this, that uses characters in this string to generate a serial number: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; This can be changed by the user. Now, I…
Solomon Closson
  • 6,111
  • 14
  • 73
  • 115
2
votes
1 answer

find all the negative numbers in the array using array_map php

I have this test array $test = array(-10,20,40,-30,50,-60); I want the output to be $out = array (-10, -30, -60); Here's my solution: $temp = array(); function neg($x) { if ($x <0) { $temp[] = $x; return $x; …
Wild Widow
  • 2,359
  • 3
  • 22
  • 34
2
votes
1 answer

How can I use parameters/arguments for my callback function in array_map()?

I'm using this code to get an array from a csv file: array_map('str_getcsv', 'file.csv') But how do I set delimeter for str_getcsv() when using it in array_map function?
Einius
  • 1,352
  • 2
  • 20
  • 45
2
votes
3 answers

php | array_map replacement

I'm looking for the way to replace my array. My first array below: $arr1 = Array ( [0] => stdClass Object ( [values] => Array ( [0] => stdClass Object ( …
XTRUST.ORG
  • 3,280
  • 4
  • 34
  • 60
2
votes
1 answer

Is there any opposite function of array_map() in PHP?

Array_Map applies callback to all elements of a supplied array. I want to know if there is any function that applies an array of callbacks to any mixed variable (or all elements of an array)? For example, array_map works as: function array_map(…
Hamid Sarfraz
  • 1,089
  • 1
  • 14
  • 34
2
votes
3 answers

array_map two callback functions as one

Here is my codes: $sc = 'hello 8491241 some text 6254841 some text 568241 414844:412'; preg_match_all('/[0-9]{5,10}/', $sc, $matches1); preg_match_all('/[0-9]{5,10}:[0-9]{1,5}/', $sc, $matches2); function cub1($match) { return array( …
user2203703
  • 1,955
  • 4
  • 22
  • 36
2
votes
0 answers

php array_map does not find external class

I have 2 classes Helper and Movies. I Helper i have a static method flats(). Now i need to use the flats() method inside my Movies class: array_map(['Helper', 'flats'], $data); But there error come: Warning: array_map() expects parameter 1 to be a…
nutzt
  • 2,773
  • 3
  • 17
  • 26
2
votes
5 answers

Return Highest Key value Pair inside of an Array Map in Clojure

I have a function that is returning the following array map: {{498 3} {-498 3}, {99 2} {-99 2}, {499 2} {-499 2}, {100 1} {-100 1}} And I'd like to return 2, because it has the highest key, 499. Whats the best way to do this?
user3245240
  • 265
  • 4
  • 10
2
votes
3 answers

mysql_real_escape_string and array_map returns blank strings?

I haven't yet learned how to use parameterized queries (which according to some other posts on this site is something that I absolutely need to do first thing tomorrow morning) and I want to get a whack of form data into a query, escaped. Twice, I…
armadadrive
  • 963
  • 2
  • 11
  • 42
2
votes
1 answer

array_map showing syntax error in eclipse , but code is working fine

I'm working with codeIgniter, and when I use PHP function array_map, it shows a syntax error, but the code is working fine. Is array_map a deprecated function? Why is this happening? Should I avoid using array_map? Thanks in advance.
Kanishka Panamaldeniya
  • 17,302
  • 31
  • 123
  • 193
1
vote
1 answer

array_map to simplify array, what am I doing wrong?

I have a function which returns an array of arrays when querying a table, each 'subarray' is a row in the table, now I want to create a 'fetchColumn' function to transform my resulting array from this: Array( [0] => Array( 'column' =>…
olanod
  • 30,306
  • 7
  • 46
  • 75
1
vote
0 answers

How do a map in bootstrap carousel and keep one item active?

I want map my Games array and keep one game carousel-item-active, how ? I do a fetchCall on my API and i want display them on a carousel
{games.map((game) => (