这三种方法之间的性能差异(如果有的话)是什么,都用于将数组转换为另一个数组?
foreacharray_maplambda/closure函数array_map'静态'功能/方法为了使自己清楚,让我们看看这些例子,都做同样的事情 - 将数字数组乘以10:
$numbers = range(0, 1000);
Run Code Online (Sandbox Code Playgroud)
的foreach
$result = array();
foreach ($numbers as $number) {
$result[] = $number * 10;
}
return $result;
Run Code Online (Sandbox Code Playgroud)
用lambda映射
return array_map(function($number) {
return $number * 10;
}, $numbers);
Run Code Online (Sandbox Code Playgroud)
使用'static'函数映射,作为字符串引用传递
function tenTimes($number) {
return $number * 10;
}
return array_map('tenTimes', $numbers);
Run Code Online (Sandbox Code Playgroud)
还有其他方法吗?我将很高兴听到上述案例之间的所有差异,以及为什么应该使用其他人而不是其他人的任何输入.
我想做这样的事情:
class Cls {
function fun($php) {
return 'The rain in Spain.';
}
}
$ar = array(1,2,3);
$instance = new Cls();
print_r(array_map('$instance->fun', $ar));
// ^ this won't work
但是array_map的第一个参数应该是函数的名称.我想避免在$ instance-> fun周围写一个包装函数,但看起来这似乎不太可能.真的吗?
我正在尝试创建一个类来处理数组,但我似乎无法array_map()在其中工作.
<?php
//Create the test array
$array = array(1,2,3,4,5,6,7,8,9,10);
//create the test class
class test {
//variable to save array inside class
public $classarray;
//function to call array_map function with the given array
public function adding($data) {
$this->classarray = array_map($this->dash(), $data);
}
// dash function to add a - to both sides of the number of the input array
public function dash($item) {
$item2 = '-' . $item . '-';
return $item2;
}
}
// dumps start array …Run Code Online (Sandbox Code Playgroud) 我array_map在我的php应用程序中使用函数.我像这样定义了array_map函数.
$ratingID = $this->db->insert_id();
$rated_item_array = array_map(function ($a) {
return $a + array('RatingID' => $ratingID);
}, $rated_item_array);
Run Code Online (Sandbox Code Playgroud)
Php通知来了
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: ratingID
Run Code Online (Sandbox Code Playgroud)
当我打印$ratingID.我正确打印了值,因此定义了$ ratingID.为什么array_map功能不明确?我的$rated_item_array是
Array
(
[0] => Array
(
[RatingFactorPreferenceID] => 1,
[PreferenceID] => 45,
[RatedValue] => 1,
[CreatedOn] => 1326790338,
[CreatedBy] => 25
)
[1] => Array
(
[RatingFactorPreferenceID] => 2,
[PreferenceID] => 45,
[RatedValue] => 1,
[CreatedOn] => 1326790338,
[CreatedBy] => 25 …Run Code Online (Sandbox Code Playgroud) 使用:
for($i=1; $i<= 10000; ++$i) {
$arrayOfNumbers[] = rand(1, 99999);
}
Run Code Online (Sandbox Code Playgroud)
有人可以解释为什么存在这样的速度差异:
array_map(array($maxHeap, 'insert'), $arrayOfNumbers);
# Avg Time: 0.92856907844543s
# against
foreach($arrayOfNumbers as $number) {
$maxHeap->insert($number);
}
# Avg Time: 1.3148670101166
Run Code Online (Sandbox Code Playgroud)
$maxHeap 是一个对象 class MaxHeap extends SplMaxHeap
假设我有简单的类:
class MyClass {
private $_prop;
public function getProp() {return $this->_prop;}
[....]
}
Run Code Online (Sandbox Code Playgroud)
现在我想要做的不是在范围内的MyClasssom是从MyClass($objs)的对象数组中获取$ _prop的数组.这当然可以用这样的代码完成:
$props = array();
foreach ($objs as $obj) {
$props[] = $obj->getProp();
}
Run Code Online (Sandbox Code Playgroud)
然而,这需要引用一些行,特别是当以这种方式格式化时(我必须使用这种格式).所以问题是:如果可以使用array_map来做到这一点?一种方法是使用create function,但我真的不喜欢它在php中(php中的lambdas至少很尴尬,如果我理解它的性能就像evaled代码那样,但性能不在这里).我已经厌倦了搜索,但没有找到任何定义性的答案.但我有点觉得这是不可能的.我尝试了类似的东西array_map(array('MyClass', 'getProp'), $objs),但这不起作用,因为方法不是静态的.
编辑:我正在使用php 5.3.
我有一个名为Collectionstore 的类,它存储相同类型的对象.
Collection实现阵列接口:Iterator,ArrayAccess,SeekableIterator,和Countable.
我想将一个Collection对象作为数组参数传递给array_map函数.但这失败了,错误
PHP警告:array_map():参数#2应该是一个数组
我可以通过实现其他/更多接口来实现这一点,以便将Collection对象视为数组吗?
我在这里测试了内联匿名函数array_map
并且它工作但当我尝试使用$ user_meta它不起作用.
$user_meta = Array ( [interest] => Array ( [0] => Array ) [type] =>
Array ( [0] => Array ) [user_status] => Array ( [0] => deny)
[firstname] => Array ( [0] => ) [lastname] => Array ( [0] => B )
[email] => email@cc.com )
$user_meta = array_map(function($a) { return $a[0]; },$user_meta);
Run Code Online (Sandbox Code Playgroud)
"解析错误:语法错误,意外的T_FUNCTION,期待')'in"
是否可以array_map在str_replace不调用其他函数的情况下结合使用str_replace?
例如:
array_map(str_replace(' ', '-', XXXXX), $myArr);
我在理解这个概念时遇到了问题Array.map.我确实去过Mozilla和Tutorials Point,但他们提供的信息非常有限.
这就是我的使用方式Array.map.这有点复杂(涉及到一些d3.js;只是忽略它)
var mapCell = function (row) {
return columns.map(function(column) {
return { column : column, value : getColumnCell(row, column) }
})
}
//getColumnCell is a function defined in my code
//columns is array defined at the top of my code
Run Code Online (Sandbox Code Playgroud)
我不明白这段代码到底在做什么.我知道它返回了一个新阵列和东西,但这部分有点棘手!
如果你想通过我的代码:http://jsfiddle.net/ddfsb/2/
更新1
我正在使用控制台来实际了解代码中发生的事情.看看提供的答案,我已经清楚地理解了它的概念array.map.现在剩下的唯一部分是参数行和列,但是行和行之间存在差异,并且提供了小提琴中的列和列
var rows//completely ok
var columns//completely ok
funcion(row)//here,source of row is unknown.getColumncell function utilizes this parameter further making it more critical
function(column)//source of column is unknown..getColumncell …Run Code Online (Sandbox Code Playgroud)