Converting a nested array into a series of separate arrays

Hello,

I’m currently working with a series of multi-dimensional arrays, and have run into a case in which I need to convert these into a series of single-dimensional arrays. For example, given the following:

Array (
    [0] => cardiff
    [1] => ely-cardiff
    [2] => st-fagans
    [3] => peterston
    [4] => llantrissant
    [5] => pencoed
    [6] => Array (
            [0] => bridgend
            [1] => Array (
                    [0] => tondu
                    [1] => cefn-cribwr
                    [2] => pyle
                    [3] => porthcawl
                )
            [2] => llangonoyd
            [3] => maesteg
        )
    [7] => pyle
    [8] => port-talbot
    [9] => briton-ferry
)

I would like to break this apart and create a series of separate arrays, e.g.

// Array 1
Array (
    [0] => cardiff
    [1] => ely-cardiff
    [2] => st-fagans
    [3] => peterston
    [4] => llantrissant
    [5] => pencoed
    [6] => bridgend
    [7] => pyle
    [8] => port-talbot
    [9] => briton-ferry
)

// Array 2
Array (
    [0] => bridgend
    [1] => tondu
    [2] => llangonoyd
    [3] => maesteg
)

// Array 3
Array (
    [0] => tondu
    [1] => cefn-cribwr
    [2] => pyle
    [3] => porthcawl
)

Note, that the first value in each new array, is the first value in the nested array. The original array could potentially have several levels of nesting, so I need some sort of recursive function. This is probably covering basic programming concepts, but couldn’t find an answer elsewhere, so thought I would ask in this friendly forum :slight_smile:

Much help appreciated!

Paul

I think you can use extract so they get treated as an array in their own right. See here. Might be worth asking the question on StackOverflow.

Extract() needs an associative array. I’d ask that question on Stackoverflow, as @jimbobrjames already suggested.

You probably need an array of arrays as a result, so that you can work with it afterwards.

This should do the trick. I guess you could also do some magic with array_walk or similar but this one is probably more readable :slight_smile:

function array_flatten(array $array) {
  $chunks = [];

  foreach ($array as $key => $value) {
    if (is_array($value)) {
      $chunks = array_merge($chunks, array_flatten($value));
    } else {
      $chunks[0][$key] = $value;
    }
  }

  return $chunks;
}
3 Likes

Nice solution @lukaskleinschmidt! It just need one small change to achieve @paulrobertlloyd’s goal:

function array_flatten(array $array) {
    $chunks = [];

    foreach ($array as $key => $value) {
        if (is_array($value)) {
+           $chunks[0][$key] = $value[0];
            $chunks = array_merge($chunks, array_flatten($value));
        } else {
            $chunks[0][$key] = $value;
        }
    }

    return $chunks;
}
3 Likes

Perfect guys! (and some more)

This worked perfectly. Thank-you @lukaskleinschmidt, @pedroborges, and everyone else. I love this community! :slight_smile:

Paul