2011-10-08 12 views
8

Devo rimuovere le voci vuote sugli array multilivello. Per ora posso rimuovere le voci con sub-array vuoti, ma non le matrici vuote ... confuso, così faccio ... Penso che il codice contribuirà a spiegare meglio ...PHP - Come rimuovere le voci vuote di un array in modo ricorsivo?

<?php 

/** 
* 
* This function remove empty entries on arrays 
* @param array $array 
*/ 
function removeEmptysFromArray($array) { 

    $filtered = array_filter($array, 'removeEmptyItems'); 
    return $filtered; 
} 

/** 
* 
* This is a Callback function to use in array_filter() 
* @param array $item 
*/ 
function removeEmptyItems($item) { 

    if (is_array($item)) { 
     return array_filter($item, 'removeEmptyItems'); 
    } 

    if (!empty($item)) { 
     return true; 
    } 
} 


$raw = array(
    'firstname' => 'Foo', 
    'lastname' => 'Bar', 
    'nickname' => '', 
    'birthdate' => array( 
     'day' => '', 
     'month' => '', 
     'year' => '', 
    ), 
    'likes' => array(
     'cars' => array('Subaru Impreza WRX STi', 'Mitsubishi Evo', 'Nissan GTR'), 
     'bikes' => array(), 
    ), 
); 

print_r(removeEmptysFromArray($raw)); 

?> 

Ok, questo codice rimuoverà "nickname", "birthdate" ma non rimuoverà "bikes" che hanno una matrice vuota.

La mia domanda è ... Come rimuovere la voce "bici"?

migliori saluti,

Ci dispiace per il mio inglese ...

+0

Perché vuoi rimuoverli? – John

risposta

24

provare questo codice:

<?php 
function array_remove_empty($haystack) 
{ 
    foreach ($haystack as $key => $value) { 
     if (is_array($value)) { 
      $haystack[$key] = array_remove_empty($haystack[$key]); 
     } 

     if (empty($haystack[$key])) { 
      unset($haystack[$key]); 
     } 
    } 

    return $haystack; 
} 

$raw = array(
    'firstname' => 'Foo', 
    'lastname' => 'Bar', 
    'nickname' => '', 
    'birthdate' => array(
     'day' => '', 
     'month' => '', 
     'year' => '', 
    ), 
    'likes' => array(
     'cars' => array('Subaru Impreza WRX STi', 'Mitsubishi Evo', 'Nissan GTR'), 
     'bikes' => array(), 
    ), 
); 

print_r(array_remove_empty($raw)); 
+0

Ottimo, funziona! Grazie per l'aiuto. –

+2

Grazie, l'ho appena usato. Un'aggiunta: se si desidera rimuovere anche le stringhe "con spazi vuoti" nei valori dell'array, aggiungere una piccola clausola "elseif": if (is_array ($ value)) { $ haystack [$ key] = array_remove_empty ($ haystack [$ chiave]); } elseif (is_string ($ haystack [$ key])) { $ haystack [$ key] = trim (valore $); } in questo modo l'array (array ("", "')) verrà restituito come vuoto anche. –

+0

Usa sempre \ _ \ _ FUNCTION__ in chiamate ricorsive per rendere la tua funzione portatile. –

-2

Se si desidera array_filter lavorare in modo ricorsivo, è necessario assicurarsi che il le chiamate successive possono modificare gli elementi nidificati più profondi dell'array. Breve: dovrai passarlo per riferimento:

function removeEmptyItems(&$item) { 
    if (is_array($item) && $item) { 
     $item = array_filter(&$item, 'removeEmptyItems'); 
    } 

    return !!$item; 
} 
1

Penso che questo dovrebbe risolvere il tuo problema.

$retArray =array_filter($array, arrayFilter); 

function arrayFilter($array) { 
    if(!empty($array)) { 
     return array_filter($array); 
    } 
} 
0

Ecco la mia soluzione, rimuoverà lista esattamente specificato di valori vuoti in modo ricorsivo:

/** 
* Remove elements from array by exact values list recursively 
* 
* @param array $haystack 
* @param array $values 
* 
* @return array 
*/ 
function array_remove_by_values(array $haystack, array $values) 
{ 
    foreach ($haystack as $key => $value) { 
     if (is_array($value)) { 
      $haystack[$key] = array_remove_by_values($haystack[$key], $values); 
     } 

     if (in_array($haystack[$key], $values, true)) { 
      unset($haystack[$key]); 
     } 
    } 

    return $haystack; 
} 

è possibile utilizzarlo in questo modo:

$clean = array_remove_by_values($raw, ['', null, []]); 

Nota, rimuove sub vuoto array se si passa [] come uno dei valori.

Problemi correlati