Is there Java HashMap equivalent in PHP?

前端 未结 5 1652

I need PHP object similar to HashMap in Java, but I didn\'t find when I googled, so if someone knows how I can mimic HashMaps in PHP, help would be appreciated.

5条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-02 12:31

    Depending on what you want you might be interested in the SPL Object Storage class.

    http://php.net/manual/en/class.splobjectstorage.php

    It lets you use objects as keys, has an interface to count, get the hash and other goodies.

    $s = new SplObjectStorage;
    $o1 = new stdClass;
    $o2 = new stdClass;
    $o2->foo = 'bar';
    
    $s[$o1] = 'baz';
    $s[$o2] = 'bingo';
    
    echo $s[$o1]; // 'baz'
    echo $s[$o2]; // 'bingo'
    

提交回复
热议问题