Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
PHP Call methods of objects in array using 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);
This will be slower than a ‘for’ loop since it invokes one function for every element −
function map($obj) {
return $obj->getProperty();
}
$props = array_map('map', $objs);
Alternatively, for PHP versions before 5.3, the below code can be used −
function map($obj) {
return $obj-> getProperty ();
}
$props = array_map('map', $objs);
}
The getProperty function will be called on all the objects and the specific property is displayed. Alternative −
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 value’s utf8 encoded data will be displayed.
Advertisements
