在php中取消设置数组键。

在php中取消设置数组键。,php,arrays,Php,Arrays,我有一个如下所示的数组,我想删除具有空值的键,并将它们输出到名为test的新数组存储中 $array = array( '6' => array( 'null' =>array( 'null'=>array( '11:04'=>array( 'id' => '22' ) ) ), '1'=>array(

我有一个如下所示的数组,我想删除具有空值的键,并将它们输出到名为
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']);
 }
}

我创建了一个条件,当
$comp
等于null时,它将使用null$comp取消数组设置。但是当我把它放到
test
数组中时,它就不工作了。我做错了什么。谢谢
有点不清楚您的结果应该是什么,但我认为您需要一个递归迭代器:

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

有点不清楚您的结果应该是什么,但我认为您需要一个递归迭代器:

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

将print_r($array)添加到代码中,您将看到密钥正在从该数组中删除。是的,我知道。但是我需要将$array的结果应用到$results。您能演示最终数组的预期输出是什么样子的吗?你可能需要一个递归迭代器来做你想做的事情。将print_r($array)添加到你的代码中,你会看到密钥正在从该数组中删除。是的,我知道。但是我需要将$array的结果应用到$results。您能演示最终数组的预期输出是什么样子的吗?您可能需要一个递归迭代器来执行您想要的操作。