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'?

3
  • Look here: stackoverflow.com/questions/7551386/… and here stackoverflow.com/questions/10835511/remove-subarray-with-php Commented Dec 26, 2012 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. Commented Dec 26, 2012 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? Commented Dec 26, 2012 at 13:55

2 Answers 2

6
$array = array_filter($array, function (array $i) {
    return count($i) != 1 || key($i) != 'intensity';
});
Sign up to request clarification or add additional context in comments.

Comments

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

2 Comments

Are you sure that it's wise to remove elements from the array as you're iterating over it?
@Jack: should be Ok in case of foreach loops (PHP will work on a copy of the array during the loop) check stackoverflow.com/questions/10057671/how-foreach-actually-works and some comments here: stackoverflow.com/questions/2852344/… however, deceze's answer is not 'ambiguous' on that point and IMO is the way to go if you use PHP 5.3+

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.