Я пытаюсь создать класс, с помощью которого я смогу создать массив с любой глубиной массивов / значений. Но я не могу найти способ сделать это. Любые предложения / советы / подсказки будут хороши.
Вот коды
class CreateArray{
public function __construct() {
}
private $array = [];
public function add_value($value){
$this->array[] = $value;
return $this;
}
public function sub_array_start() {
// What to do here?!
return $this;
}
public function sub_array_end() {
// What to do here?!
return $this;
}
public function get() {
return $this->array;
}
}
$d_array = new CreateArray();
$d_array = $d_array
->add_value([1, 2, 3])
->sub_array_start()
->add_value([3, 2, 8])
->add_value([4, 2, 5])
->sub_array_start()
->add_value([4, 2, 5])
->add_value([3, 2, 8])
->sub_array_end()
->add_value([4, 2, 5])
->sub_array_end()
->add_value([1, 2, 3])
->add_value([1, 2, 3])
->sub_array_start()
->add_value([3, 2, 8])
->add_value([4, 2, 5])
->sub_array_start()
->sub_array_start()
->add_value([3, 2, 8])
->add_value([4, 2, 5])
->sub_array_start()
->add_value([3, 2, 8])
->add_value([4, 2, 5])
->sub_array_end()
->sub_array_end()
->add_value([3, 2, 8])
->add_value([4, 2, 5])
->sub_array_end()
->sub_array_end();
->get();
print_r($d_array);
Приведенные выше коды должны создать такой массив -
Array
(
[0] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[1] => Array
(
[0] => Array
(
[0] => 3
[1] => 2
[2] => 8
)
[1] => Array
(
[0] => 4
[1] => 2
[2] => 5
)
[2] => Array
(
[0] => Array
(
[0] => 4
[1] => 2
[2] => 5
)
[1] => Array
(
[0] => 3
[1] => 2
[2] => 8
)
)
[3] => Array
(
[0] => 4
[1] => 2
[2] => 5
)
)
[2] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[3] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[4] => Array
(
[0] => Array
(
[0] => 3
[1] => 2
[2] => 8
)
[1] => Array
(
[0] => 4
[1] => 2
[2] => 5
)
[2] => Array
(
[0] => Array
(
[0] => Array
(
[0] => 3
[1] => 2
[2] => 8
)
[1] => Array
(
[0] => 4
[1] => 2
[2] => 5
)
[2] => Array
(
[0] => Array
(
[0] => 3
[1] => 2
[2] => 8
)
[1] => Array
(
[0] => 4
[1] => 2
[2] => 5
)
)
)
[1] => Array
(
[0] => 3
[1] => 2
[2] => 8
)
[2] => Array
(
[0] => 4
[1] => 2
[2] => 5
)
)
)
)