<?php
class foo
{
protected $_data;
public function __construct()
{
}
public function __get($name)
{
switch ($name)
{
case 'things':
return $this->_data['things'];
break;
}
}
public function __set($name, $value)
{
switch ($name)
{
case 'things':
$this->_data['things'] = $value;
break;
}
}
}
$f = new foo();
$f->things[0]['Fruits'] = ["apple", "orange", "banana"];
$f->things[0]['Vegetables'] = ["carrot", "tomato", "potato"];
$f->things[1]['Fruits'] = ["grapes", "strawberry"];
$f->things[1]['Vegetables'] = ["cabbage", "radish", "lettuce"];
print_r($f);
?>
答案 0 :(得分:0)
您的代码应如下所示: -
class foo
{
protected $_data = array();
public function __get($name){
// return $this->_data($name);
}
public function __set($name, $value){
$key = key($value);
$key1 = key($value[$key]);
if(isset($this->_data[$name][$key])){
$this->_data[$name][$key][$key1] = $value[$key][$key1];
}else{
$this->_data[$name][$key] = $value[$key];
}
}
}
$f = new foo();
$f->things = [0=>['Fruits'=>["apple", "orange", "banana"]]];
$f->things = [1=>['Fruits'=>["grapes", "strawberry"]]];
$f->things = [0=>['Vegetables'=>["carrot", "tomato", "potato"]]];
$f->things = [1=>['Vegetables'=>["cabbage", "radish", "lettuce"]]];
$f->things = [2=>['Fruits'=>["mango","beet"]]];
$f->things = [2=>['Vegetables'=>["beetroot","broad beans"]]];
echo '<pre>'; print_r($f);
<强>输出强>: -
foo Object
(
[_data:protected] => Array
(
[things] => Array
(
[0] => Array
(
[Fruits] => Array
(
[0] => apple
[1] => orange
[2] => banana
)
[Vegetables] => Array
(
[0] => carrot
[1] => tomato
[2] => potato
)
)
[1] => Array
(
[Fruits] => Array
(
[0] => grapes
[1] => strawberry
)
[Vegetables] => Array
(
[0] => cabbage
[1] => radish
[2] => lettuce
)
)
[2] => Array
(
[Fruits] => Array
(
[0] => mango
)
[Vegetables] => Array
(
[0] => roseflower
)
)
)
)
)
希望它会对你有所帮助: - )