2016-06-23 13 views
1

Ich habe ein Array wie unten und ich möchte den Schlüssel mit Null-Wert entfernen und sie in einem neuen Array-Speicher mit dem Namen test.Unset-Array-Schlüssel in PHP.

$array = array(
'6' => array(
    'null' =>array(
     'null'=>array(
       '11:04'=>array(
        'id' => '22' 
       ) 
     ) 
    ), 
    '1'=>array(
     '2'=>array(
       '11:04'=>array(
        'id' => '22' 
       ) 
     ) 
    ), 
) 
); 

Dies ist der Code, den ich bisher gemacht habe:

foreach($array as $devp => $dev){ 
foreach($dev as $comp => $com){ 

if($comp == 'null'){ 
    unset($array[$devp][$comp]); 
} 
foreach($com as $areap=>$area){ 
    foreach($area as $timep=>$time){ 
    foreach($time as $k=> $v){ 
     $results[$devp][$comp][$areap][$timep]['active']= 'true'; 
    } 
    } 
} 
unset($array[$devp]['null']); 
} 

}

print_r($results); 

ich eine Bedingung erstellt haben, dass, wenn die $comp gleich ist es auf null wird unset das Array mit null $ comp. Aber wenn ich es in test Array einfügen, funktioniert es nicht. Was mache ich falsch. Vielen Dank. https://eval.in/593908

+0

hinzufügen print_r ($ array), um Ihren Code, und Sie werden sehen, dass der Schlüssel aus diesem Array entfernt wird. –

+0

ja ich weiß. Aber ich muss die Ergebnisse von $ array auf $ results anwenden. – useruseruser

+0

Können Sie demonstrieren, wie die erwartete Ausgabe der endgültigen Arrays aussehen würde? Sie würden wahrscheinlich einen rekursiven Iterator wünschen, um zu tun, was Sie wollen. – Rasclatt

Antwort

0

Es ist ein wenig unklar, was Ihre Ergebnisse sollten aber ich denke, Sie eine rekursive Iterator brauchen:

// Given this array 
$array = array(
    '6' => array(
     'null' =>array(
      'null'=>array(
       '11:04'=>array(
        'id' => '22' 
       ) 
      ) 
     ), 
     '1'=>array(
      '2'=>array(
       '11:04'=>array(
        'id' => '22' 
       ) 
      ) 
     ), 
     '3'=>array(
      'null'=>array(
       '11:04'=>array(
        'id' => '22' 
       ) 
      ) 
     ) 
    ) 
); 

// This is the iterator function 
function iterate($array,$find,&$new) 
    { 
     // Loop through array 
     foreach($array as $key => $value) { 
      // If the current key is the same as what you are looking for 
      if($key == $find) 
       // Assign to the new array 
       $new[] = $array[$key]; 
      // If the key is not the same 
      else { 
       // If the value is another array 
       if(is_array($value)) 
        // Use this function to drive down into the array 
        $return[$key] = iterate($value,$find,$new); 
       // If not array, assign the value 
       else 
        $return[$key] = $value; 
      } 
     } 
     // return the $return if set 
     if(isset($return)) 
      return $return; 
    } 

// Store null arrays 
$new = array(); 
// Create filtered array 
$newd = iterate($array,'null',$new); 

print_r($new); 
print_r($newd); 

Gibt Ihnen:

Array 
(
    [0] => Array 
     (
      [null] => Array 
       (
        [11:04] => Array 
         (
          [id] => 22 
         ) 
       ) 
     ) 

    [1] => Array 
     (
      [11:04] => Array 
       (
        [id] => 22 
       ) 
     ) 
) 

Array 
(
    [6] => Array 
     (
      [1] => Array 
       (
        [2] => Array 
         (
          [11:04] => Array 
           (
            [id] => 22 
           ) 
         ) 
       ) 
      [3] => 
     )  
)