0

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:

  1. 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
  2. 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 ?

2
  • 1
    so you have an array that may or may not contain a key components, but any of the components can also contain a key components and so on. The goal is to "flatten" all components in a single array ? Commented Jul 31, 2019 at 6:11
  • Yes you are totaly correct. Was not easy to explain :) Commented Jul 31, 2019 at 6:21

1 Answer 1

1

If I understood you correctly recursion can help:

function extractComponents($arrayWithComponents)
{
    $components = [];

    if (!isset($arrayWithComponents['components']) || !is_array($arrayWithComponents['components'])) {
        return $components;
    }

    foreach ($arrayWithComponents['components'] as $component) {
        if (isset($component['columns'])) {
            foreach ($component['columns'] as $column) {
                $components = array_merge($components, extractComponents($column));
            }
        } else {
            $components = array_merge($components, extractComponents($component));
            unset($component['components']);
            $components[] = $component;
        }
    }

    return $components;
}

var_dump(extractComponents($data));

Example.

Sign up to request clarification or add additional context in comments.

2 Comments

It was exactly what I was looking for. Thank you very much. :)
Anyway just for improve the final array, the function should to exclude the element that contain Components as type=panel, tabs :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.