1

I want to merge all sub arrays of an array in an older project that requires >=5.3.3.

The array mentioned above looks like this:

$array = Array (
Array
(
    [0] => 26
    [1] => 644
)
Array
(
    [0] => 20
    [1] => 26
    [2] => 644
)
Array
(
    [0] => 26
)
Array
(
    [0] => 47
)
Array
(
    [0] => 47
    [1] => 3
    [2] => 18
)
Array
(
    [0] => 26
    [1] => 18
)
Array
(
    [0] => 26
    [1] => 644
    [2] => 24
    [3] => 198
    [4] => 8
    [5] => 6
    [6] => 41
    [7] => 31
)
Array
(
    [0] => 26
    [1] => 644
    [2] => 24
    [3] => 198
    [4] => 12
    [5] => 25
    [6] => 41
    [7] => 31
)
Array
(
    [0] => 198
)
Array
(
    [0] => 198
)
Array
(
    [0] => 899
))

Now, I'm wondering how I could merge all of those Subrrays into one that looks like this:

Array
(
    [1] => 26
    [2] => 644
    [3] => 20
    [4] => 26
    [5] => 644
    [6] => 26
    [7] => 47
    [8] => 47
    [9] => 3
    [10] => 18
    [11] => 26
    [12] => 18
    [13] => 26
    [14] => 644
    [15] => 24
    [16] => 198
    [17] => 8
    [18] => 6
    [19] => 41
    [20] => 31
    [21] => 26
    [22] => 644
    [23] => 24
    [24] => 198
    [25] => 12
    [26] => 25
    [27] => 41
    [28] => 31
    [29] => 198
    [30] => 198
    [31] => 899
)

I know how this could work on a more up to date PHP version. So far on this older version I've tried the following:

print_r(array_merge($array, $emptyArray));

But I get the exact same Array returned.

I also tried something like this:

$result_arr = array();
foreach ($array as $sub_arr) $result_arr = array_merge($result_arr, $sub_arr);
$result_arr = array_unique($result_arr);

print_r($result_arr);

Which tells me my second argument is not an array?

I'm a bit confused and hope someone can shed some light on this issue.

networkcore
  • 135
  • 1
  • 11

3 Answers3

1

this function, I think, will do it well.

function array_flatten(array $array)
{
    $result = array();
    foreach ($array as $key => $value) {

        if (is_array($value)) {

            $result = array_merge($result, array_flatten($value));

        } else {
            $result = array_merge($result, array($key => $value));
        }
    }
    return $result;
}

All you have to do is send your array as a parameter of the function, and display the result.

$arrayToFlatt = Array(
    Array(26, 644),
    Array(20, 26, 644),
    Array(26),
    Array(47),
    Array(47, 3, 18),
    Array(26, 18),
    Array(26, 644, 24, 198, 8, 6, 41, 31),
    Array(26, 644, 24, 198, 12, 25, 41, 31),
    Array(198),
    Array(198),
    Array(899)
);
echo '<pre>';
print_r(array_flatten($arrayToFlatt));
echo '</pre>';

Working since PHP 4

  • Thank you very much for sharing your approach. However, this returns to me an array of arrays as well. – networkcore Jan 13 '20 at 16:41
  • Thank you! I now get an error and this is an error that I have gotten a few times trying to solve this: array_merge(): Argument #2 is not an array -> talking about this specific line: $result = array_merge($result, $value); – networkcore Jan 13 '20 at 16:52
  • If I remove 'else' I get this: Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) – networkcore Jan 13 '20 at 16:56
  • I got the result you want with my function – Yona Smilevitch Jan 13 '20 at 16:58
1

You could use array_walk_recursive(), which will visit each leaf node of the input array, then add this value to an output array...

$output = array();
array_walk_recursive($array, function ($data) use (&$output) {
    $output[] = $data;
});
print_r($output);
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
0

Your foreach approach is fine, and works for me. But I don't see why you are filtering with array_unique.

<?php

$data = 
[
    [1,3,5,7],
    [2,4,6,8],
    [1,2,4,5]
];

$output = [];
foreach ($data as $sub) {
    $output = array_merge($output, $sub);
}
var_export($output);

Output:

array (
  0 => 1,
  1 => 3,
  2 => 5,
  3 => 7,
  4 => 2,
  5 => 4,
  6 => 6,
  7 => 8,
  8 => 1,
  9 => 2,
  10 => 4,
  11 => 5,
)

To flatten with the splat (Php 5.6):

$output = array_merge(...$data);

For pre-splat versions:

$output = call_user_func_array('array_merge', $data);

Or array_reduce:

$output = array_reduce($data, 'array_merge', []);

If in doubt foreach:

$output = [];
foreach($data as $v)
    foreach($v as $n)
        $output[] = $n;

All the above result in the same zero indexed array.

Progrock
  • 7,373
  • 1
  • 19
  • 25