简体   繁体   English

array_values不适用于ArrayAccess对象

[英]array_values doesn't work with ArrayAccess object

array_values() doesn't work with ArrayAccess object. array_values()不适用于ArrayAccess对象。 neither does array_keys() array_keys()也没有

why? 为什么?

if I can access $object['key'] I should be able to do all kind of array operations 如果我可以访问$object['key']我应该能够进行所有类型的数组操作

No, you've misunderstood the utility of ArrayAccess. 不,你误解了ArrayAccess的实用程序。 It isn't just a sort of wrapper for an array. 它不仅仅是一种数组包装器。 Yes, the standard example for implementing it uses a private $array variable whose functionality is wrapped by the class, but that isn't a particularly useful one. 是的,实现它的标准示例使用私有$array变量,其功能由类包装,但这不是特别有用的。 Often, you may as well just use an array. 通常,您也可以使用数组。

One good example of ArrayAccess is when the script doesn't know what variables are available. ArrayAccess的一个很好的例子是当脚本不知道哪些变量可用时。

As a fairly silly example, imagine an object that worked with a remote server. 作为一个相当愚蠢的例子,想象一个与远程服务器一起工作的对象。 Resources on that server can be read, updated and deleted using an API across a network. 可以使用网络上的API读取,更新和删除该服务器上的资源。 A programmer decides they want to wrap that functionality with array-like syntax, so $foo['bar'] = 'foobar' sets the bar resource on that server to foobar and echo $foo['bar'] retrieves it. 一个程序员决定他们想用类似数组的语法包装那个功能,所以$foo['bar'] = 'foobar'将该服务器上的bar资源设置为foobarecho $foo['bar']检索它。 The script has no way of finding out what keys or values are present without trying all possible values. 如果不尝试所有可能的值,脚本无法找到存在的键或值。

So ArrayAccess allows the use of array syntax for setting, updating, retrieving or deleting from an object with array-like syntax: no more, no less. 因此,ArrayAccess允许使用数组语法来设置,更新,检索或删除具有类似数组语法的对象:不多也不少。

Another interface, Countable , allows the use of count() . 另一个接口Countable允许使用count() You could use both interfaces on the same class. 您可以在同一个类上使用这两个接口。 Ideally, there would be more such interfaces, perhaps including those that can do array_values or array_keys , but currently they don't exist. 理想情况下,会有更多这样的接口,可能包括那些可以执行array_valuesarray_keys ,但目前它们不存在。

ArrayAccess is very limited. ArrayAccess非常有限。 It does not allow the use of native array_ functions (no existing interface does). 它不允许使用本机array_函数(没有现有的接口)。

If you need to do more array-like operations on your object, then you are essentially creating a collection. 如果您需要对对象执行更多类似数组的操作,那么您实际上是在创建一个集合。 A collection should be manipulated by its methods. 应该通过其方法操纵集合。

So, create an object and extend ArrayObject . 因此,创建一个对象并扩展ArrayObject This implements IteratorAggregate , Traversable , ArrayAccess , Serializable and Countable . 这实现了IteratorAggregateTraversableArrayAccessSerializableCountable

If you need the keys, simply add an array_keys method: 如果需要密钥,只需添加array_keys方法:

public function array_keys($search_value = null, $strict = false)
{
    return call_user_func_array('array_keys', array($this->getArrayCopy(), $search_value, $strict));
}

Then you can: 然后你可以:

foreach ($object->array_keys() as $key) {
    echo $object[$key];
}

The ArrayObject / ArrayAccess allows objects to work as arrays, but they're still objects. ArrayObject / ArrayAccess允许对象作为数组工作,但它们仍然是对象。 So instead of array_keys() (which work only on arrays) you should use get_object_vars() , for example: 因此,不应使用array_keys() (仅适用于数组),而应使用get_object_vars() ,例如:

var_dump(array_keys(get_object_vars($ArrObj)));

or convert your ArrayObject by casting it into array by (array) $ArrObj , eg: 或者通过(array) $ArrObj将它转换为数组来转换你的(array) $ArrObj ,例如:

var_dump(array_keys((array)$ArrObj));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM