Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I've got this array:

array(5) {
  [0]=>
  array(4) {
    ["nachricht"]=>
    string(9) "blablaaaa"
    ["user"]=>
    string(15) "334607943355808"
    ["datum"]=>
    string(16) "18.09.2014 11:13"
    ["deleted"]=>
    string(0) ""
  }
  [1]=>
  array(4) {
    ["nachricht"]=>
    string(3) "joo"
    ["user"]=>
    string(15) "334607943355808"
    ["datum"]=>
    string(16) "18.09.2014 11:56"
    ["deleted"]=>
    string(15) "334607943355808"
  }
  [2]=>
  array(4) {
    ["nachricht"]=>
    string(4) "noma"
    ["user"]=>
    string(15) "334607943355808"
    ["datum"]=>
    string(16) "18.09.2014 11:56"
    ["deleted"]=>
    string(0) ""
  }
  [3]=>
  array(4) {
    ["nachricht"]=>
    string(4) "test"
    ["user"]=>
    string(15) "334607943355808"
    ["datum"]=>
    string(16) "18.09.2014 11:56"
    ["deleted"]=>
    string(0) ""
  }
  [4]=>
  array(4) {
    ["nachricht"]=>
    string(4) "doh!"
    ["user"]=>
    string(15) "334607943355808"
    ["datum"]=>
    string(16) "18.09.2014 11:56"
    ["deleted"]=>
    string(0) ""
  }
}

I want to delete all sub arrays which include the value 334607943355808 in the key "deleted" in the sub array. I got this code:

if(($key = array_search("334607943355808", $array)) !== false) {
            unset($array[$key]);
        }

from: PHP array delete by value (not key) where it's non multi-array, but how can I do it in my case?

EDIT:

I tryed it this way now:

foreach($array as $delete){
      if(($key = array_search("334607943355808", $delete)) !== false) {
                unset($delete[$key]);
      }
}

But it's not working

share|improve this question
    
you need recursion. –  Karoly Horvath Sep 18 '14 at 15:52
1  
Use a nested foreach loop and check the values to see if they contain your number? –  TheOneWhoSighs Sep 18 '14 at 15:54
    
not as flexible, but in these case it will work just as well. –  Karoly Horvath Sep 18 '14 at 15:54
    
What you need is a FOREACH loop. –  Loenix Sep 18 '14 at 16:00
    
Look my edit, please help –  user2966167 Sep 18 '14 at 16:02

2 Answers 2

up vote 0 down vote accepted

Just a simple foreach with a reference to the sub array:

foreach($array as &$sub_array) {
    if($sub_array['deleted'] == '334607943355808') {
        $sub_array = null;
    }
}

Or by key in the main array:

foreach($array as $key => $sub_array) {
    if($sub_array['deleted'] == '334607943355808') {
        unset($array[$key]);
    }
}
share|improve this answer

You can use array_map().Try this

$finalArr = array_map(function($v){
    if($v['deleted'] == '334607943355808') unset($v['deleted']);
    return $v;
}, $arr);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.