Answer the question
In order to leave comments, you need to log in
How to convert a tree into multiple flat arrays?
Hello, there is a task to validate a tree using laravel, for this there are used rules, such as:
'questions.0.answers.0.questions.0.text' => 'required'
'questions.0.answers.0.questions.1.text' => 'required'
'questions.0.answers.1.questions.0.text' => 'required'
Что будете есть?
Торт
Пирог
Суп
Какой?
Борщ
Солянка
['Что будете есть?']
['Что будете есть?', 'Торт']
['Что будете есть?', 'Пирог']
['Что будете есть?', 'Суп']
['Что будете есть?', 'Суп', 'Какой?']
['Что будете есть?', 'Суп', 'Какой?', 'Борщ']
['Что будете есть?', 'Суп', 'Какой?', 'Солянка']
[
{
"text": true,
"content": "Что будете есть?",
"child": [
{
"text": true,
"content": "Торт",
},
{
"text": true,
"content": "Пирог",
},
{
"text": true,
"content": "Суп",
"child": [
{
"text": true,
"content": "Какой?",
"child": [
{
"text": true,
"content": "Борщ",
},
{
"text": true,
"content": "Солянка",
}
]
}
]
}
]
}
]
,
и так далее
Answer the question
In order to leave comments, you need to log in
Greetings!
What you're after is most likely Laravel's own Helper Arr::dot .
I stuffed your json into dot and dumped it through dd(), got:
PS Accordingly, the algorithm for generating such a thing, you can look at the function in your IDE via Ctrl + LMB. (If you are using software from JetBrains)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question