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

)

How would I remove the sub-arrays that have only 1 key, and that is 'intensity'?

ditto
  • 5,917
  • 10
  • 51
  • 88
  • Look here: http://stackoverflow.com/questions/7551386/how-to-delete-subarray-from-array-in-php and here http://stackoverflow.com/questions/10835511/remove-subarray-with-php –  Dec 26 '12 at 13:50
  • I understand how to unset an array. What I do not know how is how to target a subarray that has just 1 key and that key is a certain word. – ditto Dec 26 '12 at 13:52
  • Set up a temp array, loop through your array, keep what you want, and then overwrite the original array with the temp. What's stopping you? –  Dec 26 '12 at 13:55

2 Answers2

6
$array = array_filter($array, function (array $i) {
    return count($i) != 1 || key($i) != 'intensity';
});
deceze
  • 510,633
  • 85
  • 743
  • 889
3
foreach($yourArray as $key => $value)
  if (is_array($value) && count($value) == 1 && isset($value['intensity']))
    unset($yourArray[$key]);
Maxime Pacary
  • 22,336
  • 11
  • 85
  • 113
  • Are you sure that it's wise to remove elements from the array as you're iterating over it? –  Dec 26 '12 at 13:59
  • 1
    @Jack: should be Ok in case of foreach loops (PHP will work on a copy of the array during the loop) check http://stackoverflow.com/questions/10057671/how-foreach-actually-works and some comments here: http://stackoverflow.com/questions/2852344/unset-array-element-inside-a-foreach-loop however, deceze's answer is not 'ambiguous' on that point and IMO is the way to go if you use PHP 5.3+ – Maxime Pacary Dec 26 '12 at 14:25