array-map

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

a 夏天 提交于 2019-12-08 05:06:48
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? Something like: foreach ($arr as $v) { $foo[] = $v['foo']; $bar[] = $v['bar']; } In a lucky coincidence,

array_map 2d array to 1d associative array

懵懂的女人 提交于 2019-12-04 12:55:52
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 ($value) { return $value['ID']=>$value['Name']; }, $ResultArray); Scopey If you are using PHP5.5 then

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

依然范特西╮ 提交于 2019-12-02 20:06:39
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 Array ( [0] => Array ( [RatingFactorPreferenceID] => 1, [PreferenceID] => 45, [RatedValue] => 1,

mysql_real_escape_string and array_map returns blank strings?

本秂侑毒 提交于 2019-12-02 02:25:27
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 my existing PHP it creates a bunch of blank values. I was under the impression that mysqli_real_escape

empty() not a valid callback?

空扰寡人 提交于 2019-12-01 23:17:12
问题 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 <? $arrays = array( 'arrEmpty' => array( '','','' ), ); foreach ( $arrays as $key => $array ) { echo $key . "\n"; echo array_reduce( $array, "empty" ); var_dump( array_map("empty", $array) ); echo "\n\n"; } $ php test.php arrEmpty Warning: array_reduce(): The second argument, 'empty', should be a valid callback in /var/www/authentication_class/test.php on line 12 Warning: array

Alternatives to Pass both Key and Value By Reference:

梦想与她 提交于 2019-12-01 16:20:36
Can someone explain to me why you can't pass a key as reference? Ex: if(is_array($where)){ foreach($where as &$key => &$value){ $key = sec($key); $value = sec($value); } unset($key, $value); } Throws: Fatal error: Key element cannot be a reference in linkstest.php on line 2 Can I do something similar using array_map? All I want to do is iterate over an associative array, and escape both the key and value with my sec() function. Array map is difficult for me to understand: I have tried many things with array_map, but I can't get it to act on the keys directly. Would I get any performance

Alternatives to Pass both Key and Value By Reference:

*爱你&永不变心* 提交于 2019-12-01 15:01:02
问题 Can someone explain to me why you can't pass a key as reference? Ex: if(is_array($where)){ foreach($where as &$key => &$value){ $key = sec($key); $value = sec($value); } unset($key, $value); } Throws: Fatal error: Key element cannot be a reference in linkstest.php on line 2 Can I do something similar using array_map? All I want to do is iterate over an associative array, and escape both the key and value with my sec() function. Array map is difficult for me to understand: I have tried many

setting scope of array_map php

对着背影说爱祢 提交于 2019-12-01 10:39:05
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. When using a class method as a callback for functions like array_map() and usort() , you have to send

PHP array_map trim + parameters

馋奶兔 提交于 2019-12-01 06:12: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(" '\"")); } return $split; } Somehow I can't make this work though. I can still see double quotes in the result even

array_map on collection with array interfaces?

让人想犯罪 __ 提交于 2019-12-01 02:09:24
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? The array_map() function doesn't support a Traversable as its array argument, so you would have to perform a conversion step: array_map($fn, iterator_to