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

Combining two arrays based on values php

I was wondering, how it is possible to perform function on each array element based on value. For example, if I have two arrays: [ 0 => 'gp', 1 => 'mnp', 2 => 'pl', 3 => 'reg' ] And $translation = [ 'gp' => 'One', 'mnp' =>…
Kristīne Glode
  • 1,409
  • 4
  • 16
  • 22
0
votes
2 answers

Using Array Map in a Javascript "Class" to Return Each Item of the Array

I'm trying to build some programs for fun and learning. I have the problem below: function Hero(name) { this.name = name; this.addItem = function(item) { this.items = this.items || []; this.items.push(item); }; // ....…
Ryan Mc
  • 833
  • 1
  • 8
  • 25
0
votes
1 answer

Wrong result for array_count_values in PHP

I have an array like this - Array ( [0] => একটু [1] => আরো [2] => নয় [3] => কাছে [4] => থাকো [5] => না [6] => কাছে [7] => ডাকো [8] => না [9] => আরো [10] => কাছে [11] => আজ [12] => দুজনের [13] => প্রথম [14] => মিলনের [15] => চাঁদ [16] => তারা [17]…
Nihar
  • 333
  • 1
  • 6
  • 18
0
votes
0 answers

How to use a recursive method with arguments in context of array_map(...)?

I have an array and want to iterate it and apply a class method to every element. For this purpose one can use array_map(...) or array_walk(...). If it's a multidimensional array, array_walk_recursive(...) can be used. What I have now is following…
automatix
  • 14,018
  • 26
  • 105
  • 230
0
votes
0 answers

How can i change this foreach to native array function of php?

I want to know what other options are there other than foreach, to create the output array i want. Input array is: $arr = array( '4X2' => 86, '4X3' => 9, '4X4' => 5, '6X2' => 14, '6X4' => 1 ); Output array that i want to…
yo black
  • 127
  • 1
  • 5
0
votes
2 answers

How can i avoid pass by refrence in array map?

I've this piece of code in which I want to know is there anyway I could avoid pass by reference public function formatNumbers($numbersData){ $result = array(); array_map( function($row) use (&$result) { …
yo black
  • 127
  • 1
  • 5
0
votes
2 answers

find punctual element in array with two elements javascript

I have the next situation: 1). one array with months from 1 to 12 and values at 0: var months = [[1,0], [2,0], [3,0], [4,0], [5,0], [6,0], [7,0], [8,0], [9,0], [10,0], [11,0], [12,0], [13,0]]; 2). another small array which represents…
Limon
  • 1,772
  • 7
  • 32
  • 61
0
votes
1 answer

add new index using array map function in php without using looping function

this is my array Array ( [0] => Array ( [id] => 277558 [text_value] => Jif [response_count] => 13 [response_percentage] => 92 ) [1] => Array ( [id] =>…
0
votes
1 answer

Why is array_map failing here?

I'm attempting to apply a function to an array of animals. I want to embolden them. $arr = array('cat', 'dog'); function makemebold($item) { return "$item"; // or something more interesting... } Let's check out the original…
a coder
  • 7,530
  • 20
  • 84
  • 131
0
votes
2 answers

removing array duplicates from associative array

So i have: Array ( [animals] => Array ( [0] => horse [1] => dog [2] => dog ) [team] => Array ( [0] => cubs [1] => reds [2] => cubs …
Hard Fitness
  • 452
  • 3
  • 9
  • 24
0
votes
1 answer

Array map mysqli_query with array of SQLs

I upgraded an script from MySQL to MySQLi extension in PHP. The code looks like: array_map('mysqli_query', $sqls); And, I met an error there which was working on the past. Warning: mysqli_query() expects at least 2 parameters, 1 given in file.php…
Bimal Poudel
  • 1,214
  • 2
  • 18
  • 41
0
votes
4 answers

PHP: Array add index if not exists

I have an array with these entries: $row['10']['something'] = "21"; $row['5']['something'] = "42"; I want to make a string from index 0 to 10. But only index "5" and "10" are set. However I want a string like this: 0, 0, 0, 0, 42, 0, 0, 0, 0,…
yoshi
  • 1,287
  • 3
  • 15
  • 28
0
votes
2 answers

Build an array of times using array_map - possibility of associative array?

I am trying to populate a dropdown list with quarter hour times. The key being the option value, and the value being the option text. private function quarterHourTimes() { $formatter = function ($time) { return date('g:ia', $time); …
Gravy
  • 12,264
  • 26
  • 124
  • 193
0
votes
1 answer

PHP: syntax error, unexpected '[' when using array_map

I've been creating a php script for a project and have been running it on my development server which is running PHP 5.4.17 I need to move it over to my production server which is running PHP 5.4.19 When using the array_map function on my…
Sal B.
  • 77
  • 2
  • 12
0
votes
1 answer

Modify array php using array_map

How to modify the following array using array map. So far I couldn't achieve the wanted results so here is my question: Convert this: Array ( [Open+Sans:300,300italic,regular,italic,600,600italic,700,700italic,800,800italic] => Open Sans …
Jeton R.
  • 385
  • 9
  • 23