<?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);
?>
Asked
Active
Viewed 33 times
-1

anjanesh
- 3,771
- 7
- 44
- 58
-
See: http://stackoverflow.com/q/7500148/3933332 – Rizier123 Mar 14 '16 at 09:12
-
please explain question in clear – droidev Mar 14 '16 at 09:36
1 Answers
0
Your code should be as below:-
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);
output:-
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
)
)
)
)
)
Hope it will help you :-)

Ravi Hirani
- 6,511
- 1
- 27
- 42