1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Identifying an item

Discussion in 'Development' started by Muqsit, Dec 30, 2016.

  1. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Hey there,
    I want to save an item to an array and use the isset() function to perform checks.

    PHP:
    public $array = [];

    public function 
    fromCache(Item $item){
        if (isset(
    $this->array[$item])) {
            return 
    $this->array[$item];
        } else {
            
    $this->array[$item] = [...];
            return 
    $this->array[$item];
        }
    }
    Obviously, that won't work since you can't set objects as keys (array_flip? lol?).

    All I want to do is have something that could be set as a key so that I could identify $item.
    I know you can set $item->tags as key, but that could create conflicts. Don't different items have a unique data such as entities have a unique entity ID for the session they're online?
     
  2. imYannic

    imYannic Baby Zombie

    Messages:
    113
    I think that you think wrong. You want to cache an item and want to get it again? Why don't you just use a one-dimensional array which only contains your items? You can push these items easily without requiring any identifiable key. You can also get them with in_array($item, $this->cache){}.

    Why don't you use this way?
     
  3. xBeastMode

    xBeastMode Shog Chips

    Messages:
    0
    Use spl_object_hash like this:
    PHP:
    $this->cache[spl_object_hash($item)] = $item;
     
    Thunder33345 likes this.
  4. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Thank you both!
    P.S: This is how an item stored in an array looks like.
    Code:
    !php/object "O:25:\"pocketmine\\item\\ItemBlock\":8:{s:8:\"\0*\0block\";O:20:\"pocketmine\\block\\Air\":7:{s:5:\"\0*\0id\";i:0;s:7:\"\0*\0meta\";i:0;s:11:\"boundingBox\";N;s:5:\"level\";N;s:1:\"x\";N;s:1:\"y\";N;s:1:\"z\";N;}s:5:\"\0*\0id\";i:0;s:7:\"\0*\0meta\";i:0;s:26:\"\0pocketmine\\item\\Item\0tags\";s:0:\"\";s:31:\"\0pocketmine\\item\\Item\0cachedNBT\";N;s:5:\"count\";i:0;s:13:\"\0*\0durability\";i:0;s:7:\"\0*\0name\";s:3:\"Air\";}"
    
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.