簡體   English   中英

在PHP中取消設置數組鍵。

[英]Unset array key in php.

我有一個如下所示的數組,我想刪除具有空值的鍵,並將它們輸出到名為test的新數組存儲中。

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

這是我到目前為止所做的代碼:

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

我創建了一個條件,當$comp等於null時,它將使用null $ comp取消設置數組。 但是,當我把它放在test陣列中時,它不起作用。 我究竟做錯了什么。 謝謝。 https://eval.in/593908

有點不清楚你的結果應該是什么,但我認為你需要一個遞歸迭代器:

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

給你:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM