php Remove parent level array from set of arrays a

2020-02-08 16:26发布

问题:

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

回答1:

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



回答2:

$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.

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);


回答3:

<?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;
        }
    }
}


回答4:

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
)


回答5:

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



回答6:

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 )
        )