2

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

  • 1
    your second example is still a multidimensional array. Can you confirm that this is the result that you want? Also, what have you tried so far? – kscherrer Dec 21 '17 at 07:53
  • From where have you got `[text] => 1`? – splash58 Dec 21 '17 at 07:56
  • Until you can post an input array (and expected result) where @KrisRoofe 's answer doesn't work, your question is Unclear. I have voted to close. – mickmackusa Dec 21 '17 at 13:25

1 Answers1

3

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];
});
LF00
  • 27,015
  • 29
  • 156
  • 295