2

There are many references on S/O showing various methods to flatten a multidimensional recursive array (with more than two levels). I have been through dozens (and tried most) but I'm still running into an odd problem with every one I've tried. What I am getting as a result is:

Array
(
)
Array
(
)
Array
(
)
Array
(
    [0] => 1000043
    [1] => 1000045
    [2] => 1000050
)
Array
(
)
Array
(
)
Array
(
)
Array
(
)
Array
(
    [0] => 1000030
    [1] => 1000032
    [2] => 1000058
    [3] => 1000064
) ...

But what I'm expecting is a truly flattened single array:

Array
    [0] => 1000043
    [1] => 1000045
    [2] => 1000050
    [3] => 1000030
    [4] => 1000032
    [5] => 1000058
    [6] => 1000064
)

The method I found on S/O is supposed to handle an "empty array" (which I assume is the problem) but I'm still getting the wrong output. Here is my code:

function array_flatten5(array $array)
{
    $flat = array(); // initialize return array
    $stack = array_values($array); // initialize stack
    while($stack) // process stack until done
    {
        $value = array_shift($stack);
        if (is_array($value)) // a value to further process
        {
            $stack = array_merge(array_values($value), $stack);
        }
        else // a value to take
        {
           $flat[] = $value;
        }
    }
    return $flat;
}

Could someone point out what I missing here because I'm thinking it's something simple but at this point my eyes are crossed with the number of attempts I've made. Thank you for any help you can provide.

Here is the original array. It is 4-deep:

Array ( [0] => 1000043 [1] => 1000045 [2] => 1000050 ) Array ( [0] => 1000030 [1] => 1000032 [2] => 1000058 [3] => 1000064 ) Array ( [0] => 1000041 [1] => 1000059 [2] => 1000069 ) Array ( [0] => 1000021 [1] => 1000044 [2] => 1000049 [3] => 1000071 ) Array ( [0] => 1000009 [1] => 1000013 [2] => 1000015 [3] => 1000017 [4] => 1000053 ) Array ( [0] => 1000022 [1] => 1000034 [2] => 1000070 ) Array ( [0] => 1000038 [1] => 1000047 [2] => 1000055 [3] => 1000063 ) Array ( [0] => 1000019 [1] => 1000054 [2] => 1000060 [3] => 1000066 [4] => 1000068 ) Array ( [0] => 1000006 [1] => 1000014 [2] => 1000016 [3] => 1000072 ) Array ( [0] => 1000024 [1] => 1000025 [2] => 1000046 [3] => 1000061 [4] => 1000067 ) Array ( [0] => 1000028 [1] => 1000039 [2] => 1000048 ) Array ( [0] => 1000042 [1] => 1000057 ) Array ( [0] => 1000027 [1] => 1000033 [2] => 1000036 [3] => 1000037 ) Array ( [0] => 1000008 [1] => 1000010 [2] => 1000012 [3] => 1000018 ) Array ( [0] => 1000026 [1] => 1000062 [2] => 1000065 ) Array ( [0] => 1000020 [1] => 1000023 [2] => 1000031 [3] => 1000035 [4] => 1000040 ) Array ( [0] => 1000007 [1] => 1000011 [2] => 1000029 ) Array ( [0] => 1000051 [1] => 1000052 [2] => 1000056 ) Array ( [0] => 1000001 [1] => 1000002 [2] => 1000003 [3] => 1000004 [4] => 1000005 ) Array ( [0] => 1000073 )

And here is the outcome using the array_walk_recursive suggestion ...

Array
(
)
Array
(
)
Array
(
)
Array
(
)
Array
(
)
Array
(
    [0] => 1000111
    [1] => 1000113
    [2] => 1000129
    [3] => 1000134
)
Array
(
)
Array
(
)
Array
(
    [0] => 1000012
    [1] => 1000085
)
Array
(
) ...
  • It would be helpful to see the input array that is producing this result. – Nick May 29 '20 at 14:21
  • @Nick ... Thank you for the suggestion. I have added to the original post. Much appreciated. – Neil_Tinkerer May 29 '20 at 15:01
  • Hello experts! I could really use some direction here. I'm certain this is probably something minor I'm missing but I'm just not seeing it. Could you take a few minutes and offer some suggestions? Thank you! – Neil_Tinkerer May 29 '20 at 23:10
  • The source data you have supplied is a set of 1-deep arrays. Could you please provide the original 4-deep array? – Nick May 29 '20 at 23:23

1 Answers1

0

You didn't prepare suitable array, but looking on this code you need probably just array_walk_recursive() function.

$array = [
    [1, 2, 3, 4],
    [[5, 6], [7, 8]],
    [[[9], [10]], [11]]
];

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

print_r($result);

Output:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
    [6] => 7
    [7] => 8
    [8] => 9
    [9] => 10
    [10] => 11
)
Jsowa
  • 9,104
  • 5
  • 56
  • 60
  • Thank you for your thoughts. The DB server is down right now (maintenance) so I can't test your idea but I will asap. In the meantime, could you explain your comment "You didn't prepare suitable array"? Are you referring to what I provided within my question OR are you referring to when I originally set up the SQL query (meaning the query was poorly designed in the first place)? I ask because I'm new to this and want to learn as much as possible. Thank you! – Neil_Tinkerer May 30 '20 at 09:31
  • 1
    Yes, you should paste array as PHP code. It's not possible to make any use of result of print_r. – Jsowa May 30 '20 at 09:34
  • 1
    Understood. Thank you for the clarification. I will look on S/O for examples to make sure I do this in the future. Once I'm able to test your code, I will circle back with an update. Appreciate the support! – Neil_Tinkerer May 30 '20 at 09:39
  • Did you test out the solution? – Jsowa Jun 01 '20 at 15:33
  • Thank you for your follow-up. My hosting service ran an update and corrupted my database so I've been fighting with them over the fix. As a result, I've been unable to test your solution. I am in the process of rebuilding/restoring it but can't get to it until tomorrow. This has been very frustrating for me so, again, I apologize for not closing the loop. I will update as soon as possible. Thank you! – Neil_Tinkerer Jun 01 '20 at 23:08
  • Finally got the DB rebuilt and operational again. Unfortunately, your suggestion is giving me the same output (which I will post in my original question). I am going to go back to me original query and see if I can figure this out. Thank you, again, for your support. – Neil_Tinkerer Jun 03 '20 at 15:51