是否无法在__set中将项目设置为数组?

时间:2016-03-14 09:09:19

标签: php arrays set

<?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);
?>

1 个答案:

答案 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
                                )

                        )

                )

        )

)

希望它会对你有所帮助: - )