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);
This will be slower than using a "for" loop because it Call a function per element −
function map($obj) { return $obj->getProperty(); } $props = array_map('map', $objs);
Alternatively, for versions prior to PHP 5.3, you can use the following code −
function map($obj) { return $obj-> getProperty (); } $props = array_map('map', $objs); }
will call the getProperty function on all objects and display the specific property. Instead of −
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);
the utf8 encoded data of this value will be displayed.
The above is the detailed content of How to call an object's method in an array using array_map?. For more information, please follow other related articles on the PHP Chinese website!