我有一个如下所示的数组,我想删除具有空值的键,并将它们输出到名为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
答案 0 :(得分:0)
有点不清楚你的结果应该是什么,但我认为你需要一个递归迭代器:
getActivity.new [package_name].MovieDetail.FetchTrailersTask();
给你:
// 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);