I have this Array from a json that represent a form. I have to collect all the [components] in a new array
Array
(
[components] => Array
(
[0] => Array
(
[key] => number1
[type] => number
[input] => 1
[label] => Test Number Field
)
[1] => Array
(
[key] => testRadioFiled
[type] => radio
[label] => Test Radio Filed
[values] => Array
(
[0] => Array
(
[label] => First
[value] => first
[shortcut] =>
)
)
)
[2] => Array
(
[key] => testSelectBoxField
[type] => selectboxes
[input] => 1
[label] => Test Select Box Field
[values] => Array
(
[0] => Array
(
[label] => First
[value] => first
[shortcut] =>
)
)
)
[3] => Array
(
[key] => testSelectField
[data] => Array
(
[values] => Array
(
[0] => Array
(
[label] => First
[value] => first
)
)
)
[type] => select
[input] => 1
[label] => Test Select Single Field
)
[4] => Array
(
[key] => testPanel
[type] => panel
[input] =>
[label] => Panel
[title] => Test Panel
[components] => Array
(
[0] => Array
(
[key] => testSelectMultipleField
[data] => Array
(
[values] => Array
(
[0] => Array
(
[label] => First
[value] => first
)
[1] => Array
(
[label] => Second
[value] => second
)
[2] => Array
(
[label] => Third
[value] => third
)
)
)
[mask] =>
[sort] =>
[type] => select
[input] => 1
[label] => Test Select Multiple Field
)
[1] => Array
(
[key] => testColumns
[type] => columns
[label] => Test Columns
[columns] => Array
(
[0] => Array
(
[key] => column
[type] => column
[label] => Column
[components] => Array
(
[0] => Array
(
[key] => radio
[type] => radio
[input] => 1
[label] => Radio
[values] => Array
(
[0] => Array
(
[label] => first
[value] => first
[shortcut] =>
)
[1] => Array
(
[label] => second
[value] => second
[shortcut] =>
)
[2] => Array
(
[label] => third
[value] => third
[shortcut] =>
)
)
)
)
)
[1] => Array
(
[key] => column
[type] => column
[label] => Column
[components] => Array
(
[0] => Array
(
[key] => testSelectField2
[tab] => 1
[data] => Array
(
[values] => Array
(
[0] => Array
(
[label] => First
[value] => first
)
[1] => Array
(
[label] => Second
[value] => second
)
[2] => Array
(
[label] => Third
[value] => third
)
)
)
[type] => select
[input] => 1
[label] => Test Select Single Field
)
)
)
)
)
)
[collapsible] =>
)
[5] => Array
(
[key] => tabs2
[mask] =>
[type] => tabs
[input] =>
[label] => Tabs
[components] => Array
(
[0] => Array
(
[key] => tab2
[label] => Tab 1
[components] => Array
(
[0] => Array
(
[key] => columns2
[tab] => 0
[mask] =>
[type] => columns
[input] =>
[label] => Columns
[logic] => Array
(
)
[columns] => Array
(
[0] => Array
(
[key] => column
[type] => column
[input] =>
[label] => Column
[components] => Array
(
[0] => Array
(
[key] => number4
[type] => number
[input] => 1
[label] => Test Number Field in Column
)
)
)
[1] => Array
(
[key] => column
[type] => column
[input] =>
[label] => Column
[components] => Array
(
[0] => Array
(
[key] => number3
[type] => number
[input] => 1
[label] => Test Number Field in Column
)
)
)
)
)
)
)
[1] => Array
(
[key] => tab2
[label] => Tab 2
[components] => Array
(
[0] => Array
(
[key] => testSelectField1
[tab] => 1
[data] => Array
(
[values] => Array
(
[0] => Array
(
[label] => First
[value] => first
)
[1] => Array
(
[label] => Second
[value] => second
)
[2] => Array
(
[label] => Third
[value] => third
)
)
)
[type] => select
[input] => 1
[label] => Test Select Single Field
)
)
)
)
)
)
)
I solved making a serious of foreach and in each loop I used logic:
- If not exist an array "components" -> check if is type columns
- if yes check if exist "components" and saved in new array
- if not save in new array
- If exist again check apply the same condition of point 1 this for 3 or 4 sublevel.
This is the final array that I need
Array
(
[0] => Array
(
[key] => number1
[type] => number
[input] => 1
[label] => Test Number Field
)
[1] => Array
(
[key] => testRadioFiled
[type] => radio
[label] => Test Radio Filed
[values] => Array
(
[0] => Array
(
[label] => First
[value] => first
[shortcut] =>
)
)
)
......
[2] => Array
(
[key] => testSelectMultipleField
[data] => Array
(
[values] => Array
(
[0] => Array
(
[label] => First
[value] => first
)
[1] => Array
(
[label] => Second
[value] => second
)
[2] => Array
(
[label] => Third
[value] => third
)
)
)
[mask] =>
[sort] =>
[type] => select
[input] => 1
[label] => Test Select Multiple Field
)
}
This code make the job but can't be the final solution.
$test_array = Array();
foreach($data['components'] AS $comp_1){
if(!isset($comp_1['components'])){
if($comp_1['type'] != 'columns'){
$test_array[] = $comp_1;
} else {
foreach($comp_1['columns'] as $col_1){
if(!isset($col_1['components'])){
$test_array[] = $col_1;
} else {
foreach($col_1['components'] AS $col_comp_1){
if(!isset($col_comp_1['components'])){
$test_array[] = $col_comp_1;
}
}
}
}
}
} else {
foreach($comp_1['components'] AS $comp_2){
if(!isset($comp_2['components'])){
if($comp_2['type'] != 'columns'){
$test_array[] = $comp_2;
} else {
foreach($comp_2['columns'] as $col_2){
if(!isset($col_2['components'])){
$test_array[] = $col_2;
} else {
foreach($col_2['components'] AS $col_comp_2){
if(!isset($col_comp_2['components'])){
$test_array[] = $col_comp_2;
}
}
}
}
}
} else {
foreach($comp_2['components'] AS $comp_3){
if(!isset($comp_3['components'])){
if($comp_3['type'] != 'columns'){
$test_array[] = $comp_3;
} else {
foreach($comp_3['columns'] as $col_3){
if(!isset($col_3['components'])){
$test_array[] = $col_3;
} else {
foreach($col_3['components'] AS $col_comp_3){
if(!isset($col_comp_3['components'])){
$test_array[] = $col_comp_3;
}
}
}
}
}
}
}
}
}
}
}
My question is there is a way for extract all the "components" array that of coarse take care if not contain other "components" array ?
components, but any of the components can also contain a keycomponentsand so on. The goal is to "flatten" all components in a single array ?