turn multidimensional array to normal

I have array like this

Array
(
    [0] => Array
        (
            [text] => 1
        )
    [1] => Array
       (
         [0] => Array
            (
                [text] => 2
            )

         [1] => Array
            (
                [0] => Array
                    (
                        [text] => 3
                    )

            )

         [2] => Array
            (
                [text] => 4
            )

     )

    [2] => Array
    (
        [text] => 5
    )

)

i dont know number of dimensions, there can be many of them. Also key of target subarrays can be not 'text', but its always text key, not numerical. How do i turn this array into array like this?

 Array
(
[0] => Array
    (
        [text] => 1
    )

[1] => Array
    (
        [text] => 2
    )
[2] => Array
    (
        [text] => 3
    )
[3] => Array
    (
        [text] => 4
    )
[4] => Array
    (
        [text] => 5
    )

)

UPDATE: Okay, i didnt explain, and didnt understand whole question by myself. The thing is that array is formed by recursion i have one function:

public static function goToAction($action)
{
    $actions = array();
    $logic = file_get_contents('../../logic/logic.json');
    $logic_array = json_decode($logic, true);
    unset($logic);
    if (!isset($logic_array[$action])) {
        return false;
    } else {
        foreach ($logic_array[$action] as $action) {
            $actions[] = self::parseActionType($action);
        }
    }

    return $actions;
}

and then my array ($data) is forming here

public static function parseActionType($actions)
{
    $data = array();
    foreach ($actions as $key => $action) {
        switch ($key) {
            case 'goto': {
       $goto_actions = self::goToAction($action);
       foreach ($goto_actions as $goto_action) {
                    $data[]= $goto_action;
                }
         } break;
       ....
        }
     }

so these functions can call each other, and there can be recursion, and as I understood when this happens, this code $data[] = $goto_action puts all of received actions in one element, but I need to put each $goto_action in differend element of $data array

Upvotes: 2

Views: 75

Answers (1)

LF-DevJourney
LF-DevJourney

Reputation: 28529

You can use array_walk_recursive to traverse all the value. live demo

$result = [];
array_walk_recursive($array, function($v, $k) use(&$result){
    $result[] = [$k => $v];
});

Upvotes: 3

Related Questions