array-map

array_map 2d array to 1d associative array

坚强是说给别人听的谎言 提交于 2019-12-13 12:09:29
问题 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 elegant/efficient solution to transform it to { [1] => "Name1", [2] => "Name2", [3] => "Name3" } I know I could loop through and create the array that way, but i feel like that may be less efficient than something like a fancy array_map. Basically I want something like... array_map( function

PHP Use array map to reOrder an array

无人久伴 提交于 2019-12-13 09:19:13
问题 I need to reOrder this array with one function. From (My actual array): array:2 [▼ 0 => array:2 [▼ "way" => 0 "period" => "MONTH" ] 1 => array:2 [▼ "way" => 1 "period" => "3MONTHS" ] ] To (I would like this array): array:2 [▼ 0 => array:1 [▼ "MONTH" => 0 ] 1 => array:1 [▼ "3MONTHS" => 1 ] ] Can I do that with array_map() function? 回答1: You can simply use foreach instead like as foreach($your_arr as &$v){ $v = [$v["period"] => $v["way"]]; } print_r($your_arr); Or using array_map $your_arr =

Memory leak?! Is Garbage Collector doing right when using 'create_function' within 'array_map'?

守給你的承諾、 提交于 2019-12-12 09:42:12
问题 I found following solution here on StackOverflow to get an array of a specific object property from array of objects: PHP - Extracting a property from an array of objects The proposed solution is to use array_map and within create a function with create_function as following: $catIds = array_map(create_function('$o', 'return $o->id;'), $objects); What happens?: array_map runs through each array element in this case a stdClass object. First it creates a function like this: function($o) {

Looking for array_map equivalent to work on keys in associative arrays

穿精又带淫゛_ 提交于 2019-12-11 23:05:22
问题 Suppose I have an associative array: $array = array( "key1" => "value", "key2" => "value2"); And I wanted to make the keys all uppercase. How would I do than in a generalized way (meaning I could apply a user defined function to apply to the key names)? 回答1: You can use the array_change_key_case function of php <?php $input_array = array("FirSt" => 1, "SecOnd" => 4); print_r(array_change_key_case($input_array, CASE_UPPER)); ?> 回答2: Amazingly, there's an array_change_key_case function. 回答3:

Is the 'use' keyword available for function closures pre 5.3?

我们两清 提交于 2019-12-11 08:07:05
问题 Hell, I can't even FIND the documentation for 'use' on the PHP site (other than in the context of namespaces - nice job ambiguating a keyword BTW). Can someone confirm that function() use ($foo) { } is only available in 5.3 and later? And where did you find that documented? As an added bonus, how would you code around not being able to use 'use' (eg: with create_function($args, $funcname) as the callback for array_map())? 回答1: Closures are introduced in 5.3, thus use in conjunction with

PHP sum up array entries where two keys have the same value

依然范特西╮ 提交于 2019-12-11 06:17:25
问题 I have the following array, in which I want to sum up the total_volume for all entries where the source and the target are the same. Array ( [0] => Array ( [source] => ABC [target] => DEF [total_volume] => 10 ) [1] => Array ( [source] => ABC [target] => GHI [total_volume] => 5 ) [2] => Array ( [source] => ABC [target] => DEF [total_volume] => 5 ) ) The resulting array should look like this: ResultArray ( [0] => Array ( [source] => ABC [target] => DEF [total_volume] => 15 ) [1] => Array (

php array_map changes the first key to zero when it was originally one

浪子不回头ぞ 提交于 2019-12-11 06:14:49
问题 I have an array whick first key starts with one, and I need it like that. //first iteration of $collections $collections[1] = $data1; $collections[2] = $data2; ... //It does not have to start with zero for my own purposes So I do the stuff needed like this: //count($collections) = 56; $collections = array_map(function($array)use($other_vars){ //more stuff here //finally return: return $arr + ['newVariable'=$newVal]; },$collections); When var_dump($collections); the first key is one, which is

Multidimensional array addition

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-10 19:03:11
问题 Suppose that I have two arrays: $v_4 = array(array(1,2,3),array(4,5,6)); $v_5 = array(array(7,8,9),array(10,11,12)); How should I construct an addition function to add across these arrays so as to get: $new_array = array(array(8,10,12),array(14,16,18)); I know I need to utilise array_map somehow, but I am unsure how to proceed in the multidimensional case. 回答1: You can use $new = array(); foreach(array_map(null, $v_4, $v_5) as $var) { $data = call_user_func_array("array_map", array_merge

Call class method from inside array_map anonymous function

蓝咒 提交于 2019-12-10 17:19:08
问题 I am trying to call one of my object's methods from within an array_map anonymous function. So far I am receiving the expected error of: Fatal error: Using $this when not in object context in... I know why I am getting this error, I just don't know a way to achieve what I am trying to... Does anybody have any suggestions? Here is my current code: // Loop through the data and ensure the numbers are formatted correctly array_map(function($value){ return $this->some_method($value,'value',false);

Is there a way to send parameters into a callback function without creating my own function first?

北城以北 提交于 2019-12-10 14:03:27
问题 I have an array of values that I would like to run through htmlspecialchars but with an argument such as this: $param = htmlspecialchars($param, ENT_QUOTES); The problem is, I have an array of values that I want to run htmlspecialchars on: $array = array_map('htmlspecialchars', $array); and I would like to know if there is a way to pass ENT_QUOTES into the array_map callback? I can always use my own function that uses htmlspecialchars, but it would be nice if there was a way to do this