array-map

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

核能气质少年 提交于 2020-01-24 19:46:31
问题 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 find one. 回答1: You could check for undefined and take '-' , otherwise the value and use Array#map for getting a new array. var array = [1, 2, undefined, undefined, 7, undefined], result = array.map(v => v === undefined ? '-' : v); console.log(result);

Why does array_map() with null as callback create an “array of arrays”?

牧云@^-^@ 提交于 2019-12-23 08:26:28
问题 Today I learned about a special case of array_map() in PHP, which is mentioned as a side note in the documentation: Example #4 Creating an array of arrays <?php $a = array(1, 2, 3, 4, 5); $b = array("one", "two", "three", "four", "five"); $c = array("uno", "dos", "tres", "cuatro", "cinco"); $d = array_map(null, $a, $b, $c); print_r($d); ?> The above example will output: Array ( [0] => Array ( [0] => 1 [1] => one [2] => uno ) [1] => Array ( [0] => 2 [1] => two [2] => dos ) [2] => Array ( [0] =

How can I efficiently split an array into its associative key arrays?

冷暖自知 提交于 2019-12-23 04:39:07
问题 How can I split a single array into it's sub-keys? $arr = array( 0 => array( 'foo' => '1', 'bar' => 'A' ), 1 => array( 'foo' => '2', 'bar' => 'B' ), 2 => array( 'foo' => '3', 'bar' => 'C' ) ); What is the most efficient way to return an array of foo and bar separately? I need to get here: $foo = array('1','2','3'); $bar = array('A','B','C'); I'm hoping there's a clever way to do this using array_map or something similar. Any ideas? Or do I have to loop through and build each array that way?

PHP error. Why is “variable undefined” inside array_map?

不问归期 提交于 2019-12-20 09:48:30
问题 I am using array_map function in my php application. I defined the array_map function like this. $ratingID = $this->db->insert_id(); $rated_item_array = array_map(function ($a) { return $a + array('RatingID' => $ratingID); }, $rated_item_array); Php notice comes A PHP Error was encountered Severity: Notice Message: Undefined variable: ratingID When i print the $ratingID . i prints the value correctly , so $ratingID is defined. Why it is undfined in array_map function? My $rated_item_array is

mysql_real_escape_string and array_map returns blank strings?

女生的网名这么多〃 提交于 2019-12-20 03:29:12
问题 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 have come across this solution: $_POST = array_map('mysqli_real_escape_string', $_POST); This, from what I can tell, runs all of the variables in the $_POST array through the escape function. I have seen that exact line upvoted, but when I add it to

setting scope of array_map php

大城市里の小女人 提交于 2019-12-19 09:39:26
问题 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 ); return $value; } Question: say i wanna put this function in a static class, how would i use array_map back to the scope of the static method in the class like Sanitize::stripSlashesRecursive(); Im sure this is simple but i just cant figgure it out, looked at php.net as well

PHP array_map trim + parameters

六月ゝ 毕业季﹏ 提交于 2019-12-19 09:08:11
问题 I'm using array_map to trim all my array values but I need to pass a third parameter because I need to more than just trim whitespaces so I'm passing in a third parameter. Basically I want to trim all array values of whitespaces, single quotes, and double quotes. I have a utility class where I created the function and it looks like this: public function convertToArray($string, $trim = false) { $split = explode(",", $string); if($trim) { $split = array_map("trim", $split, array(" '\"")); }

array_map on collection with array interfaces?

懵懂的女人 提交于 2019-12-19 05:02:59
问题 I have a class called Collection which stores objects of same type. Collection implements array interfaces: Iterator , ArrayAccess , SeekableIterator , and Countable . I'd like to pass a Collection object as the array argument to the array_map function. But this fails with the error PHP Warning: array_map(): Argument #2 should be an array Can I achieve this by implementing other/more interfaces, so that Collection objects are seen as arrays? 回答1: The array_map() function doesn't support a

Performance of foreach, array_map with lambda and array_map with static function

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-16 22:24:27
问题 What's the performance difference (if there is any) between these three approaches, both used to transform an array to another array? Using foreach Using array_map with lambda/closure function Using array_map with 'static' function/method Is there any other approach? To make myself clear, let's have look at the examples, all doing the same - multiplying the array of numbers by 10: $numbers = range(0, 1000); Foreach $result = array(); foreach ($numbers as $number) { $result[] = $number * 10; }