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

Find integers/floats/strings in a string of text and put in array

I have a string and want split them in an array based on type. I can extract numbers and floats like below, but is not complete to my goal var arr = "this is a string 5.86 x10‘9/l 1.90 7.00" .match(/\d+\.\d+|\d+\b|\d+(?=\w)/g) .map(function…
0
votes
1 answer

not able to use array map function in php

array_map() expects parameter 1 to be a valid callback, class 'Search' does not have a method 'wishList' $shops = array_map(array($this, 'wishList'), $shops); function wishList($shops) { print_r(shops); …
dev dev
  • 121
  • 1
  • 2
  • 17
0
votes
2 answers

How to extract Values of particular key in an object array without iterating over the array?

Let's say I have an object array call movies like below. movies = [{ id : 1,title : 'Black Panther'},{ id : 2,title : 'Avengers'},{ id : 1,title : 'Justice League'},{ id : 4,title : 'Infinity War'},{ id : 5,title : 'Spider man'}] Is there anyway I…
Thidasa Pankaja
  • 930
  • 8
  • 25
  • 44
0
votes
2 answers

PHP Apply multiple callback functions when using array_map, array_walk etc. functions

I have encountered an array of serialized values like below: Array ( [primary] => Array ( [key_type_0_key_name] => a:1:{i:0;s:5:"27232";} [key_type_1_key_name] => a:1:{i:0;s:5:"27231";} …
VST
  • 131
  • 1
  • 10
0
votes
2 answers

How to use a ArrayMap randomly

I wrote the following ArrayMap: ArrayMap arrayMap = new ArrayMap(); arrayMap.put(sprite, "Rot"); arrayMap.put(sprite1, "Braun"); arrayMap.put(sprite2, "Dunkelblau"); …
user8340536
  • 209
  • 2
  • 7
0
votes
0 answers

Could not find class 'android.util.ArrayMap', referenced from method com.android.tools

My app is runing well but after i using the ARRAYLIST i have a problem Could not find class 'android.util.ArrayMap', referenced from method com.android.tools.fd.runtime.MonkeyPatcher.monkeyPatchExistingResources. I add the support library in the…
0
votes
1 answer

Fatal error: Unsupported operand types in PHP

I have that code where i want to multiply corp_resp for corp_resp_template and sum dynamically. $total = (array_reduce((array_map(function($x, $y) { return $x * $y; }, $corp_resp,…
0
votes
1 answer

Array to string conversion in C://... in PHP

I wanna import data from CSV file and print it on a webpage using PHP. first I tried it with print_r() it worked perfectly. But, when I tried to print each value in the array individually the error "Array to string conversion" how to prevent this…
Sri Harsha
  • 123
  • 2
  • 2
  • 12
0
votes
4 answers

in php how do i make an like array("one","two","three") into $somearray["one"]["two"]["three"]?

I would like to take an array, say: array("one", "two", "three"); and make it into an array of arrays, like: $someArray["one"]["two"]["three"]; The array of arrays ($someArray) would have to be created by some sort of loop as the initial array is…
Jonathon Oates
  • 2,912
  • 3
  • 37
  • 60
0
votes
5 answers

PHP: Apply a function to multiple variables without using an array

I have a function (for ease, I'll just use count()) that I want to apply to maybe 4-5 different variables. Right now, I am doing this: $a = count($a); $b = count($b); $c = count($c); $d = count($d); Is there a better way? I know arrays can use the…
Logan Serman
  • 29,447
  • 27
  • 102
  • 141
0
votes
1 answer

Workaround for not having anonymous functions

I have PHP 5.2 and am trying to use this anonymous function $values = array_map(function ($value) use ($link){ if($value == null) return null; return mysqli_real_escape_string($link, (string)$value); }, array_values($input)); Server's…
Mercurial
  • 3,615
  • 5
  • 27
  • 52
0
votes
4 answers

Remove empty elements from associative array selectively in PHP

I have the following associative array: Array ( [0] => Array ( [0] => 18-Jul-16 [1] => 29-Jul-15 [2] => 2-Feb-16 [3] => 3301 [4] => 1800 Bimodel [5] => 5813 …
Ashy Ashcsi
  • 1,529
  • 7
  • 22
  • 54
0
votes
3 answers

converting H:i:s to minutes only but array_map() giving errors

I am trying to convert a long datatype data to time in which I am successful. In each session time array I have values like ["1276999","787878","677267"]. I passed this array in the array_map function and converted to time which is working. Now…
iqra
  • 115
  • 1
  • 12
0
votes
2 answers

Map values in array of objects

I have an array const nodes = [ { layer: '0' }, { layer: 'input' }, { layer: '0' }, { layer: 'output' }, { layer: '1' } }; I want to keep the array, but all the layer values should be changed. The input layer should have value…
Jamgreen
  • 10,329
  • 29
  • 113
  • 224
0
votes
1 answer

Implode of array of Objects

I have an easy one (I think). I have a Silex application...I created a service services.yml file, with my services with their arguments. Of coruse, arguments can be instance of another classes: services: Service: class:…
Mauro
  • 189
  • 2
  • 14