php 我可以将实例化的对象用作数组键吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4642980/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I use an instantiated Object as an Array Key?
提问by Tylo
For example:
例如:
$product = new Product("cat");
if(isset($sales[$product])){
$sales[$product]++;
}
else{
$sales[$product] = 1;
}
回答by Felix Kling
From the docs:
从文档:
Arrays and objects can not be used as keys. Doing so will result in a warning: Illegal offset type.
数组和对象不能用作键。这样做会导致警告:非法偏移类型。
You could give each instance a unique ID or override __toString()
such that it returns something unique and do e.g.
您可以给每个实例一个唯一的 ID 或覆盖__toString()
,以便它返回一些唯一的东西并执行例如
$array[(string) $instance] = 42;
回答by goat
You can use http://www.php.net/manual/en/class.splobjectstorage.php
您可以使用http://www.php.net/manual/en/class.splobjectstorage.php
$product = new Product("cat");
$sales = new SplObjectStorage();
if(isset($sales[$product])){
$sales[$product]++;
}
else{
$sales[$product] = 1;
}
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
它不是真正的数组,但具有相当数量的类似数组的功能和语法。然而,由于它是一个对象,由于其奇怪的 foreach 行为以及它与所有原生 php 数组函数的不兼容,它在 php 中表现得不合适。有时您会发现通过以下方式将其转换为真实数组很有用
$arr = iterator_to_array($sales);
so it plays nice with the rest of your codebase.
所以它和你的代码库的其余部分一起玩得很好。
回答by Tomá? Binek
There is a spl_object_hashfunction for getting unique object id as string, which can be used as array key. http://php.net/manual/en/function.spl-object-hash.php
有一个spl_object_hash函数用于将唯一对象 id 获取为字符串,它可以用作数组键。http://php.net/manual/en/function.spl-object-hash.php
回答by Oswald
Only integers and strings are allowed as array keys. You could write a class that implements ArrayAccessif you absolutely need that functionality.
只允许整数和字符串作为数组键。如果您绝对需要该功能,您可以编写一个实现ArrayAccess的类。
回答by Gabriel Glenn
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 with json_encode
.
如果对象是一个简单的预定义类,new stdClass()
则使用此类的 json 表示可能是有效的选项json_encode
。
$product = new stdClass();
$product->brand = "Acme";
$product->name = "Patator 3.14";
$product_key = json_encode($product);
if(isset($sales[$product_key])){
$sales[$product_key]++;
}
else{
$sales[$product_key] = 1;
}
But keep in mind that the equality of two objects is always a business model choice and must be carefully designed.
但请记住,两个对象的相等性始终是一种商业模式选择,必须精心设计。
回答by Gabriel Glenn
You could have two arrays:
你可以有两个数组:
Array 1 contains the keys: | Array 2 contains the values
+--------+-------------+ | +--------+------------+
| index: | value: | | | index: | value: |
| 0 | Object(key) | | | 0 | sth(value) |
| 1 | Object(key) | | | 1 | sth(value) |
+--------+-------------+ | +--------+------------+
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
您在数组 1 中搜索对象,
然后选择该对象的索引,
将其用作数组 2 的索引,然后
=> 获取值
in php code
在 php 代码中
public function getValue($ObjectIndexOfYourArray){
foreach(array1 as $key => $value) {
if($value == ObjectIndexOfYourArray){
return array2[$key];
}
}
}
I hope it helps
我希望它有帮助