Req #80799 [Com]: Support ArrayAccess in array_column()

From: Date: Tue, 02 Mar 2021 11:46:03 +0000
Subject: Req #80799 [Com]: Support ArrayAccess in array_column()
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-232469@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=80799&edit=1 ID: 80799 Comment by: manuelbrooks50 at gmail dot com Reported by: saribekyantaron at gmail dot com Summary: Support ArrayAccess in array_column() Status: Open Type: Feature/Change Request Package: Class/Object related Operating System: Any PHP Version: Next Minor Version Block user comment: N Private report: N New Comment: PHP 5 array_column doesn't work with an array of objects. Use array_map instead: $titles = array_map(function($e) { return is_object($e) ? $e->Title : $e['Title']; }, $records); PHP 7 array_column() The function now supports an array of objects as well as two-dimensional arrays. Only public properties are considered, and objects that make use of __get() for dynamic properties must also implement __isset(). https://www.mcdvoice.ltd/ Previous Comments: ------------------------------------------------------------------------ [2021-02-25 12:17:42] saribekyantaron at gmail dot com Description: ------------ --- From manual page: https://php.net/function.array-column --- The first parameter describes: A multi-dimensional array or an array of objects from which to pull a column of values from. If an array of objects is provided, then public properties can be directly pulled. In order for protected or private properties to be pulled, the class must implement both the __get() and __isset() magic methods. But I think if object's class implements interface \ArrayAccess, no need to define magic methods (set, get, isset) for allowing them to be used in array-management functions: like array_columns. I mean that Array*** interfaces can be enough (I will prefer required) to use objects or object arrays in array_*** functions. Test script: --------------- class SimpleArrayLikeClass implements \ArrayAccess { public function offsetExists($name) { return array_key_exists($name, $this->attributes()); } public function offsetGet($name) { return $this->attributes()[$name] ?? null; } // if object need to be used as array this 2 methods need to be used instead of __isset() and __get() } Expected result: ---------------- In future versions became deprecated to use objects as array, if they not implemented Array*** interfaces, instead of magic methods ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=80799&edit=1

« previous php.bugs (#232469) next »