2012-12-17 7 views
10

Io sono terribile con gli array manipolare ... dato questa struttura Voglio rimuovere la matrice di livello superiore e unire tutti i sottoinsiemi in un array piatto:php Rimuovere gamma livello padre dal set di pannelli e unire i nodi

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

alla struttura desiderata:

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

velocità è essenziale - avremo a che fare con centinaia di migliaia di risultati

+0

Vuoi davvero che il risultato sia 'array (array ('hey.com', 'you.com' , 'this.com', 'rocks.com')) '? – salathe

+0

Ho solo bisogno di un array piatto di tutti i valori effettivi, tutto qui. –

+0

Vedere la modifica appena effettuata –

risposta

13

è possibile utilizzare RecursiveArrayIterator

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

uscita

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

In questo caso non è necessario il ciclo, vedere [il mio commento sopra] (http://stackoverflow.com/questions/13920659/php-remove-parent-level-array-from-set-of-arrays-and -merge-nodi # comment19189152_13920659). – salathe

+2

Buon uso di 'iterator_to_array' + – Baba

+0

@JaredEitnier non deve essere una riga ... assicurarsi che il codice sia leggibile ... – 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>"; 
?> 

Non so come ottenere Arral come questo, ma questa soluzione è ottenere solo i valori.

[a cura] Mi dispiace, la sua più dolce:

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

Questo appiattirà l'array di esattamente su un livello. Prenderà l'input di esempio che hai fornito e produrrà l'output desiderato che hai richiesto.

Assicurarsi

  1. l'array genitore utilizza gli indici numerici
  2. l'array genitore ha almeno un elemento secondario, altrimenti si otterrà un errore di php a causa di array_merge lamentando senza argomenti.

Per coloro che si chiedono come funziona:

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

Se stai usando PHP 5.6+, l'operatore splat (...) può essere il modo più leggibile di fare questo:

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

perfetto !!! :) +1 – SagarPPanchal

0

Se l'array ha un solo livello dall'indice associativo e con un solo elemento:

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

Se i valori sono sempre allo stesso livello di profondità si può infatti utilizzare 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 
) 
Problemi correlati