'array_map'等PHP函数接受回调,它可以是一个简单的函数,也可以是一个类或对象方法:
$array2 = array_map('myFunc', $array);
或
$array2 = array_map(array($object, 'myMethod'), $array);
但是有没有一种语法可以将迭代中绑定的方法传递给当前对象(比如Prototype.js中的'invoke')?以便可以使用以下内容:
$array2 = array_map('myMethod', $array);
具有
的效果foreach($array as $obj) $array2[] = $obj->myMethod();
显然我可以使用这个表单,或者我可以编写一个包装函数来进行调用,甚至可以内联。但是,既然'myMethod'已经是一种方法,它似乎围绕着房子去做其中一种。
答案 0 :(得分:4)
目前不是。当php 5.3问世时,您可以使用以下语法:
$array2 = array_map(function($obj) { return $obj->myMethod(); }, $array);
答案 1 :(得分:2)
function obj_array_map($method, $arr_of_objects) {
$out = array();
$args = array_slice(func_get_args(), 2);
foreach ($arr_of_objects as $key => $obj) {
$out[$key] = call_user_func_array(Array($obj, $method), $args);
}
return $out;
}
// this code
$a = Array($obj1, $obj2);
obj_array_map('method', $a, 1, 2, 3);
// results in the calls:
$obj1->method(1, 2, 3);
$obj2->method(1, 2, 3);
答案 2 :(得分:1)
基本上没有。没有特殊的语法可以使这更容易。
我可以想到在PHP 5.3中这样做的一种更好的方式,因为在PHP中总是有不止一种方法可以做,但我会说它不一定是更好比你的 foreach 例子:
$x = array_reduce(
$array_of_objects,
function($val, $obj) { $val = array_merge($val, $obj->myMethod()); return $val; },
array()
);
与你的foreach一起去吧:)。
答案 3 :(得分:0)
<?php
// $obj->$method(); works, where $method is a string containing the name of the
// real method
function array_map_obj($method, $array) {
$out = array();
foreach ($array as $key => $obj)
$out[$key] = $obj->$method();
return $out;
}
// seems to work ...
class Foo {
private $y = 0;
public function __construct($x) {
$this->y = $x;
}
public function bar() {
return $this->y*2;
}
}
$objs = array();
for ($i=0; $i<20; $i++)
$objs[] = new Foo($i);
$res = array_map_obj('bar', $objs);
var_dump($res);
?>
瞧!
答案 4 :(得分:0)
这是一个有点愚蠢的答案,但您可以继承ArrayObject并使用它代替普通数组:
<?php
class ArrayTest extends ArrayObject {
public function invokeMethod() {
$result = array();
$args = func_get_args();
$method = array_shift($args);
foreach ($this as $obj) {
$result[] = call_user_func_array(array($obj, $method), $args);
}
return $result;
}
}
//example class to use
class a {
private $a;
public function __construct($a) {
$this->a = $a;
}
public function multiply($n) {
return $this->a * $n;
}
}
//use ArrayTest instance instead of array
$array = new ArrayTest();
$array[] = new a(1);
$array[] = new a(2);
$array[] = new a(3);
print_r($array->invokeMethod('multiply', 2));
输出:
Array
(
[0] => 2
[1] => 4
[2] => 6
)
答案 5 :(得分:0)
我会使用create_function()
来......嗯......在等待PHP 5.3时为array_map创建一个临时函数
$func = create_function('$o', '$o->myMethod();');
array_map($func, $objects);