Code: Select all
$array = array('some' => true, 'indexed' => true, 'data' => true, 'in' => true,
'an' => true, 'array' => true);
isset($array ['data']);Code: Select all
$array = array('some', 'unindexed', 'data', 'in', 'an', 'array');
array_search('data', $array);Now the problem, what if I want to used objects as keys. Only strings and integers are allowed.
Here is what I would really like to do:
Code: Select all
class ObjectIndex implements ArrayAccess
{
private $_objs = array();
public function offsetExists($obj)
{
return isset($this->_objs[get_obj_id($key)]);
}
public function offsetSet($key, $obj)
{
if ($key !== null) {
throw new Exception('Keys not permitted'); // we generate our own
}
$this->_obj[get_obj_id($obj)] = $obj;
}
public function offsetGet($obj)
{
return $this->_objs[get_obj_id($obj)];
}
public function offsetUnset($obj)
{
unset($this->_objs[get_obj_id($obj)]);
}
}
$index = new ObjectIndex();
$index[] = $a = new stdClass;
$index[] = $b = new Exception;
$index[] = $c = new DOMDocument;
$found = $index[$b]; // etc.Code: Select all
function get_obj_id($obj)
{
static $prefixLen = strlen('Object Id#');
if (!is_object($obj)) {
return false;
}
return (int)substr((string)$obj, $prefixLen);
}Ideas?