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

Why is carbon addMinutes doing bad addition

In Laravel environment I have a set of times that I am adding minutes to and it is not giving me what I expect. In the below array_map function there is some weird addMinutes happening $d = array_map(function ($date) { $base =…
yoyoma
  • 3,336
  • 6
  • 27
  • 42
3
votes
3 answers

PDO - array_map return objects ids in keys

hey i have array with returned keys $temp = $sth->fetchAll(PDO::FETCH_ASSOC); my result looks like this: [0] => [ 'id' = 11, 'title' => 't1' ] [1] => [ 'id' = 12, 'title' => 't2' ] if i want to return ids as key i call something…
Sławomir Kudła
  • 243
  • 2
  • 4
  • 14
3
votes
9 answers

How to replace all undefined values in an array with "-"?

I have an array like: var array = [1, 2, undefined, undefined, 7, undefined] and need to replace all undefined values with "-". The result should be: var resultArray = [1, 2, "-", "-", 7, "-"] I think there is a simple solution, but I couldn't…
Jaybruh
  • 333
  • 5
  • 14
3
votes
2 answers

Using array_map to make an alternative to print_r to produce a 'cleaner' output

I need a cleaner output than print_r gives me an array like $array = array(1,2,3,"four"=>array(4.1,4.2)); should print out somthing like this 0: 1 1: 2 2: 3 [four] 0: 4.1 1: 4.2 I've come up with this, but the array_map does not return what I…
3
votes
1 answer

Array map use variable to identify column to return

I am trying to use array map dynamically, the column in the array may change so I don't want to specify it using string but want to use a variable to specify it, it does not work. Tried all the following combinations, it returns null. $column =…
shorif2000
  • 2,582
  • 12
  • 65
  • 137
3
votes
2 answers

Create an associative array using another array as keys

I currently have the $keys array array(5) { [0] => string(9) "SessionID" [1] => string(5) "Title" [2] => string(11) "Description" [3] => string(5) "Level" [4] => string(4) "Type" } that I use as keys for the values of another…
3
votes
0 answers

Creating a Serializable ArrayMap

I'm a newbie to Android, however I'm not entirely new to Java. From what I can tell, the ArrayMap should be used over the HashMap due to less memory used/performance increase. I'm not entirely sure when I should use one over the other beyond that,…
3
votes
3 answers

Populate 2D array from each value in an array of arrays

I have a 2D array and I want to generate a formatted array. Actually I want to genetate multiple rows at a time by restructuring the input array. My 2D array: $occupied_ids = [ [8457, 6584], [9874, 4586], ]; Expected output: array ( 0 =>…
3
votes
5 answers

empty() not a valid callback?

I'm trying to use empty() in array mapping in php. I'm getting errors that it's not a valid callback. $ cat test.php array( '','','' ), ); foreach ( $arrays as $key => $array ) { echo $key…
user151841
  • 17,377
  • 29
  • 109
  • 171
3
votes
1 answer

array_map 2d array to 1d associative array

I have a 2d Array (returned from PDO MySQL DB) that is of the form { [0] => { "ID" => 1, "Name" => "Name1" }, [1] => { "ID" => 2, "Name" => "Name2" }, [2] => { "ID" => 3, "Name" => "Name3" } } Is there an…
Cheruvian
  • 5,628
  • 1
  • 24
  • 34
3
votes
0 answers

setting scope of array_map php

hey all, i use array_map from time to time to write recursive methods. for example function stripSlashesRecursive( $value ){ $value = is_array($value) ? array_map( 'stripSlashesRecursive', $value) : stripslashes( $value ); …
David Morrow
  • 8,965
  • 4
  • 29
  • 24
3
votes
2 answers

Sum corresponding elements between two multidimensional arrays

Suppose that I have two arrays, and I want to add the corresponding elements. $v_4 = [ [1, 2, 3], [4, 5, 6], ]; $v_5 = [ [7, 8, 9], [10, 11, 12], ]; How should I construct an addition function to add across these arrays to…
2
votes
3 answers

php use array as array_map first argument

I can't kind of make out the first return statement, can anybody help to explain how it works? the array_map accept a function for the first arg, but here is an array. and how does array(&$this, '_trimData') work? thanks for explaining. private…
bingjie2680
  • 7,643
  • 8
  • 45
  • 72
2
votes
1 answer

Issue with array_map callback

At first I had this (work in wamp but not in my web server) $ids = array_map(function($item) { return $item['user_id']; }, $data['student_teacher']);` So I try to convert the code to that but nothing work ( i got Array,Array,Array,Array,Array,Array…
user1029834
  • 755
  • 3
  • 9
  • 18