如何使用array_map在数组中调用对象的方法?

In PHP version 5.3, methods of objects in array can be called using the below code −
$props = array_map(function($obj){ return $obj->getProp(); }, $objs);这将比使用“for”循环慢,因为它为每个元素调用一个函数−
function map($obj) {
return $obj->getProperty();
}
$props = array_map('map', $objs);或者,对于PHP 5.3之前的版本,可以使用下面的代码 −
function map($obj) {
return $obj-> getProperty ();
}
$props = array_map('map', $objs);
}将在所有对象上调用getProperty函数,并显示特定属性。替代 −
function encode_data($val){
if(is_array($val)){
return $val = array_map('encode_data', $val);
} else {
return utf8_encode($val);
}
}
$value = array_map('encode_data', $value);
print_r($value);该值的utf8编码数据将被显示。
以上就是如何使用array_map在数组中调用对象的方法?的详细内容,更多请关注其它相关文章!
Php