18

I am terrible with manipulating arrays...given this structure I want to remove the top level array and merge all subsets into one flat array:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => hey.com
                )

            [1] => Array
                (
                    [0] => you.com
                )
        )
    [1] => Array
        (
            [0] => Array
                (
                    [0] => this.com
                )

            [1] => Array
                (
                    [0] => rocks.com
                )
        )
)

to desired structure:

Array
    (
        [0] => hey.com
        [1] => you.com
        [2] => this.com
        [3] => rocks.com
    )

Speed is essential - we will be dealing with hundreds of thousands of results

Jared Eitnier
  • 7,012
  • 12
  • 68
  • 123

6 Answers6

33
$flat = call_user_func_array('array_merge', $arr);

That will flatten the array by exactly one level. It will take the sample input you provided, and produce the desired output you asked for.

*note - the question was edited after this answer was posted. The question previously requested the following desired result:

Array
(
    [0] => Array
        (
            [0] => hey.com
            [1] => you.com
            [2] => this.com
            [3] => rocks.com
        )

)

Which is what the above array_merge() answer provides.

Make sure:

  1. your parent array uses numeric indexes
  2. the parent array has at least one child element, otherwise you'll get a php error due to array_merge complaining of no arguments.

For those who wonder how it works:

// with 
$arr = [ [1,2,3], [4,5,6] ];
// call_user_func_array('array_merge', $arr) is like calling
array_merge($arr[0], $arr[1]);

// and with 
$arr = [ [1,2,3], [4,5,6], [7,8,9] ];
// then it's like:
array_merge($arr[0], $arr[1], $arr[2]);
// and so on...

If you're using php 5.6+, the splat operator (...) can be more readable way of doing this:

$flat = array_merge(...$arr);

If you want to flatten by more than a single level, you can either use multiple nested array_merge() calls, or if you want to recursively fully flatten the structure:

// This is a great option if you don't know what depth the structure may be,
// or if the structure may contain different arrays with different depths.
$flat = iterator_to_array(new RecursiveIteratorIterator(new RecursiveArrayIterator($arr)));
goat
  • 31,486
  • 7
  • 73
  • 96
23

You can use RecursiveArrayIterator

$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($data));
$list = iterator_to_array($it,false);
var_dump($list);

Output

array (size=4)
  0 => string 'hey.com' (length=7)
  1 => string 'you.com' (length=7)
  2 => string 'this.com' (length=8)
  3 => string 'rocks.com' (length=9)

See Simple Demo

Baba
  • 94,024
  • 28
  • 166
  • 217
  • There's no need for the loop in this case, see [my comment, above](http://stackoverflow.com/questions/13920659/php-remove-parent-level-array-from-set-of-arrays-and-merge-nodes#comment19189152_13920659). – salathe Dec 17 '12 at 19:28
  • 2
    Good use of `iterator_to_array` + – Baba Dec 17 '12 at 19:31
  • 1
    My advice to the OP would be to forget using `iterator_to_array()` and just loop over the iterator. – salathe Dec 17 '12 at 19:33
2
<?php
//Very simple recoursive solution
$array = array(
    array(
        array('hey.com'),
        array('you.com')
    ),
    array(
        array('this.com'),
        array('rocks.com'),
        array(
            array('its.com'),
            array(
                array('soo.com'),
                array('deep.com')
            )
        )
    )
);

function deepValues(array $array) {
    $values = array();
    foreach($array as $level) {
        if (is_array($level)) {
            $values = array_merge($values,deepValues($level));
        } else {
            $values[] = $level;
        }
    }
    return $values;
}

$values = deepValues($array);
echo "<pre>";
print_r($values);
echo "</pre>";
?>

I dont know how to get arral like this, but this solution is get only values.

[edited] Im sorry, its sweetest:

function deepValues(array $array, array &$values) {
    foreach($array as $level) {
        if (is_array($level)) {
            deepValues($level, $values);
        } else {
            $values[] = $level;
        }
    }
}
DevMetal91
  • 340
  • 3
  • 13
1

If the values are always at the same level of depth you could indeed use array_merge:

$array = [                                                                                                                                                                                                                                 
    [
        ['hey.com'],
        ['you.com'],
    ],                                                                                                                                                                                                                
    [
        ['this.com'],
        ['rocks.com'],
    ],                                                                                                                                                                                                             
];

print_r(array_merge(... array_merge(... $array)));

Getting:

Array
(
    [0] => hey.com
    [1] => you.com
    [2] => this.com
    [3] => rocks.com
)
Mark
  • 129
  • 2
0

If your arrays always have the same previously known depth, maybe you could make good use of

http://php.net/manual/es/function.array-merge.php

0

If the array has just one level from the associative index, and with just one element:

foreach($arr as $key=>$val) { 
   if (is_array($arr[$key])) $arr[$key] = $arr[$key][0]; 
}

Getting:

[file] => Array (
            [name] => black.png
            [type] => image/png
            [tmp_name] => /tmp/phpfupdU5
            [error] => 0
            [size] => 197782
        )    

from:    

[file] => Array (
            [name] => Array
                ( [0] => black.png )
            [type] => Array
                ( [0] => image/png )
            [tmp_name] => Array
                ( [0] => /tmp/phpfupdU5 )
            [error] => Array
                ( [0] => 0 )
            [size] => Array
                ( [0] => 197782 )
        )
Banzy
  • 1,590
  • 15
  • 14