Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 无法将项设置为集合中的数组吗?_Php_Arrays_Set - Fatal编程技术网

Php 无法将项设置为集合中的数组吗?

Php 无法将项设置为集合中的数组吗?,php,arrays,set,Php,Arrays,Set,您的代码应如下所示:- <?php class foo { protected $_data; public function __construct() { } public function __get($name) { switch ($name) { case 'things': return $this->

您的代码应如下所示:-

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

希望它能帮助您:-)

请看:请清楚地解释问题如果没有它,您如何获得所需的阵列?
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
                                )

                        )

                )

        )

)