2012-12-17 77 views
10

我不善於用操作數組......給這個結構我想刪除頂層陣列和所有子集合併成一個平面數組:PHP從陣列集刪除父級陣列和合並節點

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

到所需的結構:

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

速度是至關重要的 - 我們將處理結果

成千上萬
+0

你真的想要得到的結果是'陣列(陣列(「hey.com」,「you.com」 ,'this.com','rocks.com'))'? – salathe

+0

我只需要一個所有實際值的平面陣列,就是這樣。 –

+0

查看剛編好的編輯 –

回答

13

您可以使用RecursiveArrayIterator

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

輸出

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

+0

在這種情況下,不需要循環,請參閱[我的評論,上面](http://stackoverflow.com/questions/13920659/php-remove-parent-level-array-from-set-of-arrays-and -merge節點#comment19189152_13920659)。 – salathe

+2

好用的'iterator_to_array' + – Baba

+0

@JaredEitnier它不一定是一行...確保代碼可讀... – Baba

1
<?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>"; 
?> 

我不知道怎麼去arral這樣,但這種解決方案只得到價值。

[編輯] 對不起,它的甜蜜:

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

這會使陣列變平恰好在一個水平。它將採用您提供的樣本輸入,並生成您要求的所需輸出。

確保

  1. 父陣列使用數字索引
  2. 父陣列至少有一個子元素,否則你會得到一個PHP錯誤,由於array_merge的無參數抱怨。

對於那些誰不知道它是如何工作的:

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

如果你正在使用PHP 5.6+,在圖示操作(...)可以這樣做的更可讀的方式:

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

完美! :) +1 – SagarPPanchal

0

如果陣列只有聯想指數的一個級別,並且只有一個元素:

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

如果值始終處於深度您確實可以使用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 
)