useruseruser useruseruser - 5 months ago 18
PHP Question

Unset array key in php.

I have a array like below and I want to remove the key with null value and out them into a new array storage named

test
.

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


This is the code I've done so far:

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


I've created a condition that when the
$comp
is equal to null it will unset the array with null $comp. But when I put it in
test
array it's not working. What am I doing wrong. Thanks.
https://eval.in/593908

Answer

It's a little unclear what your outcomes should be but I think you need a recursive iterator:

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

Gives you:

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