我可以使用实例化的对象作为数组键吗?
例如:
$product = new Product("cat");
if(isset($sales[$product])){
$sales[$product]++;
}
else{
$sales[$product] = 1;
}
For example:
$product = new Product("cat");
if(isset($sales[$product])){
$sales[$product]++;
}
else{
$sales[$product] = 1;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
来自 文档:
您可以为每个实例指定一个唯一的 ID 或覆盖
__toString()
这样它会返回一些独特的东西并执行例如From the docs:
You could give each instance a unique ID or override
__toString()
such that it returns something unique and do e.g.您可以使用 http://www.php.net/manual/en/class。 splobjectstorage.php
它不是一个真正的数组,但具有大量类似数组的功能和语法。然而,由于它是一个对象,由于其奇怪的 foreach 行为以及与所有本机 php 数组函数不兼容,它的行为就像与 php 不相适应。有时您会发现将其转换为真正的数组很有用,
因此它可以与代码库的其余部分很好地配合。
You can use http://www.php.net/manual/en/class.splobjectstorage.php
It's not a real array, but has a decent amount of array-like functionality and syntax. However, due to it being an object, it behaves like a misfit in php due to its odd foreach behavior, and its incompatibility with all the native php array functions. Sometimes you'll find it useful to convert it to a real array via
so it plays nice with the rest of your codebase.
有一个 spl_object_hash 函数用于获取字符串形式的唯一对象 ID,该 ID 可以用作数组键。 http://php.net/manual/en/function.spl-object -hash.php
There is a spl_object_hash function for getting unique object id as string, which can be used as array key. http://php.net/manual/en/function.spl-object-hash.php
仅允许整数和字符串作为数组键。如果您绝对需要该功能,您可以编写一个实现 ArrayAccess 的类。
Only integers and strings are allowed as array keys. You could write a class that implements ArrayAccess if you absolutely need that functionality.
您终于可以在 PHP 8.0+ 中使用对象作为数组键。但这有点谎言。它不会是您使用的常规数组,而是新的 WeakMap 类< /a>.
声明一个
new WeakMap();
,然后您可以将其填充为对象作为键。与任何基于Spl
的技巧相比,使用此方法的优点是WeakMap
能够更好地防止内存泄漏。 “弱”部分是指映射中对象的引用是“弱”的,并且一旦对象不再在范围内,就不会阻止对象被垃圾收集。WeakMap
会自动从自身中删除该对象。这是关于用例的一个很好的运行。
PHP RFC 也是一本好书。
You finally can use objects as array keys in PHP 8.0+. But that's a bit of a lie. It won't be regular arrays you're using, but the new WeakMap class.
Declare a
new WeakMap();
and then you can pack it full of objects as keys. The advantage of using this over anySpl
-based trick, is thatWeakMap
is better on memory leaks. The "weak" part refers to the fact that the object's reference in the map is "weak" and will not prevent the object from getting garbage-collected once it's no longer in-scope. TheWeakMap
will automatically remove the object from itself.Here's a good run-through on use cases.
The PHP RFC is a good read, too.
我知道这个问题很老了,并且
SplObjectStorage
有一些奇怪的行为(例如,当使用foreach
循环时)。截至今天,我编写了一个名为
linked-hash-map
的库(https://github.com/tonix-tuft/linked-hash-map)它在 PHP 中实现了关联数组/哈希映射/哈希表,并允许您使用任何 PHP 数据类型钥匙:I know this question is old, and that
SplObjectStorage
has some weird behaviour (e.g. when looping withforeach
).As of today, there is this library I wrote called
linked-hash-map
(https://github.com/tonix-tuft/linked-hash-map) which implements an associative array/hash map/hash table in PHP and allows you to use any PHP data type for the key:如果对象是使用
new stdClass()
创建的简单预定义类,则通过json_encode
使用此类的 json 表示形式可能是有效的选择。但请记住,两个对象的相等始终是一种业务模型选择,必须仔细设计。
If the object is a simple predefined classes made with
new stdClass()
it may be valid option to use the json representation of this class withjson_encode
.But keep in mind that the equality of two objects is always a business model choice and must be carefully designed.
您可以有两个数组:
您在数组 1 中搜索对象,
然后你选择该对象的索引
使用它作为数组 2 和
的索引
=> 值
获取php代码中的
我希望它有帮助
You could have two arrays:
You search for the Object in array 1,
then you pick the index of that Object
use it as index for array 2 and
=> get the value
in php code
I hope it helps