Can I use an instantiated Object as an Array Key?

前端 未结 6 1298
执笔经年
执笔经年 2020-12-15 15:47

For example:

$product = new Product(\"cat\");

if(isset($sales[$product])){
     $sales[$product]++;
}
else{
     $sales[$product] = 1;
}
6条回答
  •  轮回少年
    2020-12-15 16:10

    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

提交回复
热议问题