如何从multidimenssional数组中删除空数组?

时间:2016-01-19 16:07:55

标签: php arrays

我有一些数组,其中存在多个子数组。我想删除那些具体值为空的数组。

Array ( 
[9] => Array ( [address_id] => 9 [firstname] => [lastname] =>  ) 
[10] => Array ( [address_id] => 10 [firstname] => the [lastname] => king  ) 
[11] => Array ( [address_id] => 11 [firstname] => the [lastname] => queen  ) 
)

您可以在firstname中看到lastname[9] => Array()为空。那么如何删除那些firstname空白的数组呢?我尝试过array_filter(),但没有解决我的问题。

3 个答案:

答案 0 :(得分:2)

使用匿名函数尝试# Create a column that contains only NAs df1$Time_to_readmission <- NA # Make sure that the df is sorted by Patient_ID df1 <- df1[order(df1$Patient_ID),] # Basically, find the indices that are the same as the previous row ind <- which(c(FALSE,diff(df1$Patient_ID) == 0)) # Calculate the difference between DIS_DATE of the previous row and ADM_DATE of the index row differences <- as.Date(df1$ADM_DATE[ind], format="%m/%d/%Y") - as.Date(df1$DIS_DATE[ind - 1],format="%m/%d/%Y") # Store those differences in Time_to_readmission df1$Time_to_readmission[ind] <- differences

array_filter()

对于名字和姓氏:

$array = array_filter($array, function($v) { return !empty($v['firstname']); });

请注意,$array = array_filter($array, function($v) { return !empty($v['firstname']) && !empty($v['lastname']); }); 也是empty()0false,因此对于一个空字符串,您可能需要null或类似内容。< / p>

答案 1 :(得分:0)

假设父数组名为knockout.js,您希望循环并有条理地检查两个空字段,然后取消设置匹配的索引。

$parent

答案 2 :(得分:0)

从包含无值的嵌套数组中删除所有键,然后删除所有空嵌套数组。

max-height