PHP: how can I sort and filter an “array”, that is an Object, implementing ArrayAccess?

我的梦境 提交于 2019-12-23 13:11:31

问题


I have an object that is a collection of objects, behaving like an array. It's a database result object. Something like the following:

$users = User::get();
foreach ($users as $user)
    echo $user->name . "\n";

The $users variable is an object that implements ArrayAccess and Countable interfaces.

I'd like to sort and filter this "array", but I can't use array functions on it:

$users = User::get();
$users = array_filter($users, function($user) {return $user->source == "Twitter";});
=> Warning: array_filter() expects parameter 1 to be array, object given

How can I sort and filter this kind of object?


回答1:


You can't. The purpose of the ArrayAccess interface is not just to create an OOP wrapper for arrays (although it is often used as such), but also to allow array-like access to collections that might not even know all their elements from the beginning. Imagine a web service client, that calls a remote procedure in offsetGet() and offsetSet(). You can access arbitrary elements, but you cannot access the whole collection - this is not part of the ArrayAccess interface.

If the object also implements Traversable (via Iterator or IteratorAggregate), an array could at least be constructed from it (iterator_to_array does the job). But you still have to convert it like that, the native array functions only accept arrays.

If your object stores the data internally as an array, the most efficient solution is of course to implement a toArray() method that returns this array (and maybe calls toArray() recursively on contained objects).




回答2:


You could use ArrayObject instead of ArrayAccess, it has multiple native sort functionality:

  • ArrayObject::asort
  • ArrayObject::ksort
  • ArrayObject::natcasesort
  • ArrayObject::natsort
  • ArrayObject::uasort
  • ArrayObject::uksort


来源:https://stackoverflow.com/questions/14919172/php-how-can-i-sort-and-filter-an-array-that-is-an-object-implementing-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!