如果数组只有1个键,则从多维数组中删除数组

时间:2012-12-26 13:48:36

标签: php arrays multidimensional-array

Array
(
[9-1] => Array
    (
        [intensity] => 1
        [time] => 43932
    )

[9-2] => Array
    (
        [intensity] => 1
    )

[85-1] => Array
    (
        [intensity] => 1
        [time] => 40271
    )

[85-2] => Array
    (
        [intensity] => 1
    )

)

如何删除只有1个键的子阵列,即“强度”?

2 个答案:

答案 0 :(得分:6)

$array = array_filter($array, function (array $i) {
    return count($i) != 1 || key($i) != 'intensity';
});

答案 1 :(得分:3)

foreach($yourArray as $key => $value)
  if (is_array($value) && count($value) == 1 && isset($value['intensity']))
    unset($yourArray[$key]);