How to convert the tree into multiple flat patterns?

Hi, there is a task to validate the tree by means of laravel, for this there are rules, like these:
'questions.0.answers.0.questions.0.text' => 'required'
'questions.0.answers.0.questions.1.text' => 'required'
'questions.0.answers.1.questions.0.text' => 'required'

To generate such rules I want to convert the tree into flat arrays but can't dopetril algorithm :-(

For example, there is a tree:
What you want to eat?
Cake
Pie
Soup
What?
Soup
 Solyanka

Need to decompose it into arrays

['What will you have?']
['What will you have?', 'Cake']
['What will you have?', 'Pie']
['What will you have?', 'Soup']
['What will you have?', 'Soup', 'What?']
['What will you have?', 'Soup', 'What?', 'Soup']
['What will you have?', 'Soup', 'What?', 'Solyanka']


The data is a little more complicated:
json

[
{
 "text": true,
 "content": "something to eat?",
 "child": [
{
 "text": true,
 "content": "Cake",
},
{
 "text": true,
 "content": "Pie",
},
{
 "text": true,
 "content": "Soup",
 "child": [
{
 "text": true,
 "content": "What?",
 "child": [
{
 "text": true,
 "content": "Borsch",
},
{
 "text": true,
 "content": "Solyanka",
}
]
}
]
}
]
}
]



And put them it is necessary accordingly, in a two-dimensional array:
approximately

[[text => true, content => 'something to eat?']],
[[text => true, content => 'something to eat?'], [text => true, content => 'Cake']]
[[text => true, content => 'something to eat?'], [text => true, content => 'Pie']]
[[text => true, content => 'something to eat?'], [text => true, content => 'Soup']]
[[text => true, content => 'something to eat?'], [text => true, content => 'Soup'], [text => true, content => 'What?']]
and so on



So in laravel is there some more proper validation of the tree?

Thank you!
April 3rd 20 at 17:28
1 answer
April 3rd 20 at 17:30
Welcome!
What you need, most likely Helper Arr::dot the Laravel.

Put your json in dot and dumped via dd(), got:

5df0e0f9d1953668298532.jpeg

P. S. Accordingly, the generation algorithm like this, you can look through Ctrl + clicking on a function in your IDE. (If you use software from JetBrains)

Find more questions by tags PHPAlgorithmsLaravel