-1

I have the following structure of an array which is decoded from a JSON object. From this array result I need to fork widgets for processing. It is an unstable JSON array so the parent keys may differs in future but the widgets will not.

(
    [2] => Array
        (
            [MA] => Array
                (
                    [0] => Array
                        (
                            [activities] => Array
                                (
                                    [0] => Array
                                        (
                                            [activity_id] => 3
                                            [activity_name] => Excavation
                                            [activity_unique_id] => EXCAV-d435
                                            [created_date] => 2021-02-08 21:42:53
                                            [end_date] => 2021-02-08 21:42:08
                                            [fk_client_id] => 1
                                            [fk_main_activity] => 3
                                            [fk_project_id] => 2
                                            [relation_type] => MA-A-SA-W
                                            [start_date] => 2021-02-08 21:42:08
                                            [widgets] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [activity_mapping] => STRUC-be4c
                                                            [check_box_val] => 0
                                                            [checkbox_unique_id] => EXCAV-dceae
                                                            [checkbox_widget_id] => 5
                                                            [created_date] => 2021-02-08 21:43:12
                                                        )

                                                )

                                        )

                                )

                            [created_date] => 2021-02-08 21:42:44
                            [end_date] => 2021-02-08 21:42:08
                            [fk_client_id] => 1
                            [fk_floor_ids] => 7
                            [fk_project_id] => 2
                            [main_activity_id] => 3
                            [main_activity_name] => Structure MA1
                            [main_activity_unique_id] => STRUC-be4c
                            [relation_type] => MA-A-SA-W
                            [sequence_no] => 1
                            [start_date] => 2021-02-08 21:42:08
                            [tower_ids] => 4
                            [widgets_ids] => 
                        )

...

it is a big source So I cut shorted it. From the above result set I only need to fork the following widgets object.

[widgets] => Array
      (
           [0] => Array
           (                   
             [activity_mapping] => STRUC-be4c
             [check_box_val] => 0
             [checkbox_unique_id] => EXCAV-dceae
             [checkbox_widget_id] => 5
             [created_date] => 2021-02-08 21:43:12
             )

         )

    )

Thanks in advance.

6
  • given your array structure is not stable, suggest you use a recursive function to filter for the 'widgets' key and return its contents. What have you tried? Commented Feb 12, 2021 at 14:10
  • @berend can you give any example for that. Do you mean iterating through the array like for loop inside for loop.? Commented Feb 12, 2021 at 14:14
  • You loop through it as $key => $value and check if key is widget, if not send $value to same function Commented Feb 12, 2021 at 14:16
  • This answer should get you started. Commented Feb 12, 2021 at 14:19
  • “Thanks in advance.” - please go read How to Ask. This site is not a code-writing service. Commented Feb 12, 2021 at 14:22

3 Answers 3

0

This recursive function searches for the first occurrence of a key in a multidimensional array and returns its value.

function array_find_key(array $array, $key) {
    if (array_key_exists($key, $array)) {
        return [$key => $array[$key]];
    } else {    
        foreach($array as $item) {
            if (is_array($item) && $result = array_find_key($item, $key)) {
                return $result;
            }
        }
    }
}

$result = array_find_key($array, 'widgets');

fiddle

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

Comments

0

The answer from @id'7238 is designed to search for a qualifying value in the current level before attempting to traverse a deeper level. The nuance in my answer (which does not call array_key_exists()) is that it checks keys as it traverses a level and will go down a level as soon as a non-qualifying subarray is encountered.

While looping, if the key is found, the payload is returned (and potentially passed up the recursive stack). If a value is array type data, then the next level down is processed -- if that level contains a qualifying key, it is passed up the stack.

If there are no qualifying keys in the input array, then null will be returned.

Code: (Demo)

function searchByKey(array $array, $find) {
    foreach($array as $k => $v) {
        if ($k === $find) {
            return [$k => $v];
        }
        if (is_array($v)) {    
            $result = searchByKey($v, $find);
            if ($result) {
                return $result;
            }
        }
    }
}

var_export(searchByKey($a, 'foo'));

Comments

-1

For fun, if the nesting level will be the same just with the keys being different, this will give you the widgets array:

$result = current(current(current(current(current($array)))))['widgets'];

This will give you the array under the widgets array:

$result = current(current(current(current(current(current($array)))))['widgets']);

The first example will only work if the widgets array is 5 levels deep in the array and for the second if there is only 1 array under widgets (if there are more you will get the first).

Comments

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.