2010-12-15 73 views
3

Je suis sûr que je ne fais face qu'à un problème logique ici.Supprimer des clés d'un tableau multidimensionnel lorsque certaines valeurs spécifiques correspondent au tableau donné

J'ai un tableau comme celui-ci:

var_export($myarray); 

array (
    0 => 
    array (
    'inf:name' => 'aaa', 
    'inf:value' => '111', 
    'inf:children' => NULL, 
), 
    1 => 
    array (
    'inf:name' => 'bbb', 
    'inf:value' => '222', 
    'inf:children' => NULL, 
), 
    2 => 
    array (
    'inf:name' => 'ccc', 
    'inf:value' => '333', 
    'inf:children' => NULL, 
), 
    3 => 
    array (
    'inf:name' => 'bob', 
    'inf:children' => 
    array (
     0 => 
     array (
     'inf:name' => 'ddd', 
     'inf:value' => '444', 
     'inf:children' => NULL, 
    ), 
    ), 
), 
    4 => 
    array (
    'inf:name' => 'data', 
    'inf:children' => 
    array (
     0 => 
     array (
     'inf:name' => 'eee', 
     'inf:value' => '555', 
     'inf:children' => NULL, 
    ), 
     1 => 
     array (
     'inf:name' => 'steve', 
     'inf:value' => 'bar1', 
     'inf:children' => NULL, 
    ), 
     2 => 
     array (
     'inf:name' => 'john', 
     'inf:value' => 'bar2', 
     'inf:children' => NULL, 
    ), 
     3 => 
     array (
     'inf:name' => 'peter', 
     'inf:value' => 'bar3', 
     'inf:children' => 
     array (
      0 => 
      array (
      'inf:name' => 'fff', 
      'inf:value' => '666', 
      'inf:children' => NULL, 
     ), 
     ), 
    ), 
    ), 
), 
) 


//Or using print_r() 
print_r($myarray); 

Array 
(
    [0] => Array 
     (
      [inf:name] => aaa 
      [inf:value] => 111 
      [inf:children] => 
     ) 

    [1] => Array 
     (
      [inf:name] => bbb 
      [inf:value] => 222 
      [inf:children] => 
     ) 

    [2] => Array 
     (
      [inf:name] => ccc 
      [inf:value] => 333 
      [inf:children] => 
     ) 

    [3] => Array 
     (
      [inf:name] => bob 
      [inf:children] => Array 
       (
        [0] => Array 
         (
          [inf:name] => ddd 
          [inf:value] => 444 
          [inf:children] => 
         ) 

       ) 

     ) 

    [4] => Array 
     (
      [inf:name] => data 
      [inf:children] => Array 
       (
        [0] => Array 
         (
          [inf:name] => eee 
          [inf:value] => 555 
          [inf:children] => 
         ) 

        [1] => Array 
         (
          [inf:name] => steve 
          [inf:value] => bar1 
          [inf:children] => 
         ) 

        [2] => Array 
         (
          [inf:name] => john 
          [inf:value] => bar2 
          [inf:children] => 
         ) 

        [3] => Array 
         (
          [inf:name] => peter 
          [inf:value] => bar3 
          [inf:children] => Array 
           (
            [0] => Array 
             (
              [inf:name] => fff 
              [inf:value] => 666 
              [inf:children] => 
             ) 

           ) 

         ) 

       ) 

     ) 
) 

Je voudrais supprimer certaines touches lorsque inf:name correspond aux noms donnés.

Voici ma fonction de test:

function array_cleanup($array, $todelete) 
{ 
    foreach($array as $key => $value) 
    { 
     if(is_array($value)) 
     { 
      $array[$key] = array_cleanup($array[ $key ], $todelete); 
     } else { 
      if(sizeOf($todelete) > 0) 
      { 
       if (in_array($value[ 'inf:name' ], $todelete)) 
        unset($array[ $key ]); 
      } 
     } 
    } 
    return $array; 
} 

$newarray = array_cleanup($myarray, array("aaa", "peter", "ccc")); 

La sortie souhaitée doit être:

var_dump($newarray); 

array (
    0 => 
    array (
    'inf:name' => 'bbb', 
    'inf:value' => '222', 
    'inf:children' => NULL, 
), 
    2 => 
    array (
    'inf:name' => 'bob', 
    'inf:children' => 
    array (
     0 => 
     array (
     'inf:name' => 'ddd', 
     'inf:value' => '444', 
     'inf:children' => NULL, 
    ), 
    ), 
), 
    4 => 
    array (
    'inf:name' => 'data', 
    'inf:children' => 
    array (
     0 => 
     array (
     'inf:name' => 'eee', 
     'inf:value' => '555', 
     'inf:children' => NULL, 
    ), 
     1 => 
     array (
     'inf:name' => 'steve', 
     'inf:value' => 'bar1', 
     'inf:children' => NULL, 
    ), 
     2 => 
     array (
     'inf:name' => 'john', 
     'inf:value' => 'bar2', 
     'inf:children' => NULL, 
    ), 
    ), 
), 
) 

//or using print_r(); 
print_r($newarray); 
Array 
(
    [1] => Array 
     (
      [inf:name] => bbb 
      [inf:value] => 222 
      [inf:children] => 
     ) 

    [2] => Array 
     (
      [inf:name] => bob 
      [inf:children] => Array 
       (
        [0] => Array 
         (
          [inf:name] => ddd 
          [inf:value] => 444 
          [inf:children] => 
         ) 

       ) 

     ) 

    [3] => Array 
     (
      [inf:name] => data 
      [inf:children] => Array 
       (
        [0] => Array 
         (
          [inf:name] => eee 
          [inf:value] => 555 
          [inf:children] => 
         ) 

        [1] => Array 
         (
          [inf:name] => steve 
          [inf:value] => bar1 
          [inf:children] => 
         ) 

        [2] => Array 
         (
          [inf:name] => john 
          [inf:value] => bar2 
          [inf:children] => 
         ) 


       ) 

     ) 
) 

Les touches $newarray doivent être remis à zéro. Parce que je pourrais faire face à d'autres problèmes plus tard s'ils ne sont pas réinitialisés.

Merci les gars.

+0

Combien de niveaux peut en profondeur la 'inf: children' imbriquer? – thirtydot

+0

@stereofrog: vérifier la mise à jour, @thirtydot: Il ne devrait pas y avoir plus de quatre niveaux mais je crois que la fonction devrait être appelée récursivement afin qu'elle puisse être adaptée à toutes les entrées. – Cybrix

+0

est le tableau de xml? – ajreal

Répondre

1

Cela semble fonctionner:

<?php 

$myarray = array (
    0 => 
    array (
    'inf:name' => 'aaa', 
    'inf:value' => '111', 
    'inf:children' => NULL, 
), 
    1 => 
    array (
    'inf:name' => 'bbb', 
    'inf:value' => '222', 
    'inf:children' => NULL, 
), 
    2 => 
    array (
    'inf:name' => 'ccc', 
    'inf:value' => '333', 
    'inf:children' => NULL, 
), 
    3 => 
    array (
    'inf:name' => 'bob', 
    'inf:children' => 
    array (
     0 => 
     array (
     'inf:name' => 'ddd', 
     'inf:value' => '444', 
     'inf:children' => NULL, 
    ), 
    ), 
), 
    4 => 
    array (
    'inf:name' => 'data', 
    'inf:children' => 
    array (
     0 => 
     array (
     'inf:name' => 'eee', 
     'inf:value' => '555', 
     'inf:children' => NULL, 
    ), 
     1 => 
     array (
     'inf:name' => 'steve', 
     'inf:value' => 'bar1', 
     'inf:children' => NULL, 
    ), 
     2 => 
     array (
     'inf:name' => 'john', 
     'inf:value' => 'bar2', 
     'inf:children' => NULL, 
    ), 
     3 => 
     array (
     'inf:name' => 'peter', 
     'inf:value' => 'bar3', 
     'inf:children' => 
     array (
      0 => 
      array (
      'inf:name' => 'fff', 
      'inf:value' => '666', 
      'inf:children' => NULL, 
     ), 
     ), 
    ), 
    ), 
), 
); 





function array_cleanup($array, $todelete) { 
    foreach ($array as $key => $value) { 
     if (is_array($value)) { 
      if (in_array($value['inf:name'], $todelete)) { 
       unset($array[$key]); 
      } else { 
       $array[$key] = array_cleanup($array[$key], $todelete); 
      } 
     } 
    } 
    return $array; 
} 



$newarray = array_cleanup($myarray, array("aaa", "peter", "ccc")); 
$newarray = array_values($newarray); 

echo '<pre>'; 
var_export($newarray); 
echo '</pre>'; 

?> 
+0

En effet, cela semble fonctionner. Je vous remercie. Je vais lui donner d'autres tests ce soir. :) – Cybrix