
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
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.
- Related Articles
- Using methods of array on array of JavaScript objects?
- How to convert array into array of objects using map() and reduce() in JavaScript
- Map multiple properties in array of objects to the same array JavaScript
- How to access methods of an array of objects in JavaScript?
- Extract a property from an array of objects in PHP
- JavaScript: How to map array values without using "map" method?
- Sum 2D array in Python using map() function
- Search from an array of objects via array of string to get array of objects in JavaScript
- Array of objects to array of arrays in JavaScript
- Manipulating objects in array of objects in JavaScript
- Call methods of an object using reflection in Java
- Reset keys of array elements using PHP ?
- Creating an array of objects based on another array of objects JavaScript
- Find specific key value in array of objects using JavaScript
- Return the maximum number in each array using map JavaScript

Advertisements