아니야. 잘못된 구문입니다.
$key = 'a,b,c';
// or:
$key = serialize(array('a','b', 'c'));
// or many other things
을 그리고 ArrayAccess
또는 ArrayObject
(당신이 $key = 'a,b,c';
맡기 겠소하자 방식)를 구현됩니다 배열과 같은 수준의 구현보다 :
호버 당신은 할 수
class MyArray extends ArrayAccess {
protected $data = array();
protected &_qetViaKey($key, &$exists, $createOnNonExisting = false){
// Parse keys
$keys = array();
if(strpos($key, ',') === false){
$keys[] = $key;
} else {
$keys = explode(',', $key);
}
// Prepare variables
static $null = null;
$null = null;
$exists = true;
// Browse them
$progress = &$this->data;
foreach($keys as $key){
if(is_array($progress)){
if(isset($progress[ $key])){
$progress = $progress[ $key];
} else {
if($createOnNonExisting){
$progress[ $key] = array();
$progress = $progress[ $key];
} else {
$exists = false;
break;
}
}
} else {
throw new Exception('$item[a,b] was already set to scalar');
}
}
if($exists){
return $progress;
}
return $null;
}
public offsetExists($key){
$exists = false;
$this->_getViaKey($key, $exists, false);
return $exists;
}
// See that we aren't using reference anymore in return
public offsetGet($key){
$exists = false;
$value = $this->_getViaKey($key, $exists, false);
if(!$exists){
trigger_error(... NOTICE ...);
}
return $value;
}
public offsetSet ($key, $val){
$exists = false;
$value = $this->_getViaKey($key, $exists, true);
$value = $val;
}
}
// And use it as:
$array = MyArray();
$array['a']['b']['c'] = 3;
$array['a,b,c'] = 3;
또는 구현을 함수 :
public function &getArrayAtKey($array, $key){
// Similar to _qetViaKey
// Implement your own non existing key handling
}
뭐야? 아니면 실험보다는 숟가락 먹이로 배우는 것을 좋아하는 사람입니까? –
정확히 무엇을 성취하려고하는지 이해하기 어렵습니다. [배열에 대한 매뉴얼을 읽어보십시오] – Josh
예, 시도해 보았습니다. 아니, 나는 스푼으로 먹은 사람이 아닙니다. – Nealv