title: php unset not working with array of objects
body: <p>i have an array of objects received from mongodb i have a custom soft delete feature, for which i have to loop through the objects find the key <code>deleted_at</code> and unset it from the array this is how my array looks like if i dump it</p>
  
  <pre><code>object(MongoDB\Model\BSONArray)#41 (1) {
    [
      "storage": "ArrayObject":private
    ]=&gt;
    array(15) {
      [
        0
      ]=&gt;
      object(MongoDB\Model\BSONDocument)#26 (1) {
        [
          "storage": "ArrayObject":private
        ]=&gt;
        array(4) {
          [
            "name"
          ]=&gt;
          string(18) "BCasdaadaadadasdas"
          [
            "email"
          ]=&gt;
          string(20) "hank-zakroff@mac.com"
          [
            "mobileNo"
          ]=&gt;
          string(11) "11-22-33-44"
          [
            "deleted_at"
          ]=&gt;
          string(19) "2019-08-21 10:08:43"
        }
      }
      [
        1
      ]=&gt;
      object(MongoDB\Model\BSONDocument)#27 (1) {
        [
          "storage": "ArrayObject":private
        ]=&gt;
        array(4) {
          [
            "name"
          ]=&gt;
          string(9) "ADasdBSDS"
          [
            "email"
          ]=&gt;
          string(23) "haasdnk-zakroff@mac.com"
          [
            "mobileNo"
          ]=&gt;
          string(11) "03338142535"
          [
            "deleted_at"
          ]=&gt;
          string(19) "2019-08-21 11:08:17"
        }
      }
      [
        2
      ]=&gt;
      object(MongoDB\Model\BSONDocument)#28 (1) {
        [
          "storage": "ArrayObject":private
        ]=&gt;
        array(3) {
          [
            "name"
          ]=&gt;
          string(12) "Hank Zakroff"
          [
            "email"
          ]=&gt;
          string(20) "hank-zakroff@mac.com"
          [
            "mobileNo"
          ]=&gt;
          string(11) "03244424280"
        }
      }
      [
        3
      ]=&gt;
      object(MongoDB\Model\BSONDocument)#29 (1) {
        [
          "storage": "ArrayObject":private
        ]=&gt;
        array(3) {
          [
            "name"
          ]=&gt;
          string(12) "Hank Zakroff"
          [
            "email"
          ]=&gt;
          string(20) "hank-zakroff@mac.com"
          [
            "mobileNo"
          ]=&gt;
          string(11) "03244424281"
        }
      }
      [
        4
      ]=&gt;
      object(MongoDB\Model\BSONDocument)#30 (1) {
        [
          "storage": "ArrayObject":private
        ]=&gt;
        array(3) {
          [
            "name"
          ]=&gt;
          string(9) "Kate Bell"
          [
            "email"
          ]=&gt;
          string(17) "kate-bell@mac.com"
          [
            "mobileNo"
          ]=&gt;
          string(13) "(555)564-8583"
        }
      }
      [
        5
      ]=&gt;
      object(MongoDB\Model\BSONDocument)#31 (1) {
        [
          "storage": "ArrayObject":private
        ]=&gt;
        array(3) {
          [
            "name"
          ]=&gt;
          string(9) "Kate Bell"
          [
            "email"
          ]=&gt;
          string(17) "kate-bell@mac.com"
          [
            "mobileNo"
          ]=&gt;
          string(13) "(415)555-3695"
        }
      }
      [
        6
      ]=&gt;
      object(MongoDB\Model\BSONDocument)#32 (1) {
        [
          "storage": "ArrayObject":private
        ]=&gt;
        array(3) {
          [
            "name"
          ]=&gt;
          string(14) "Daniel Higgins"
          [
            "email"
          ]=&gt;
          string(17) "d-higgins@mac.com"
          [
            "mobileNo"
          ]=&gt;
          string(12) "555-478-7672"
        }
      }
      [
        7
      ]=&gt;
      object(MongoDB\Model\BSONDocument)#33 (1) {
        [
          "storage": "ArrayObject":private
        ]=&gt;
        array(3) {
          [
            "name"
          ]=&gt;
          string(14) "Daniel Higgins"
          [
            "email"
          ]=&gt;
          string(17) "d-higgins@mac.com"
          [
            "mobileNo"
          ]=&gt;
          string(13) "(408)555-5270"
        }
      }
      [
        8
      ]=&gt;
      object(MongoDB\Model\BSONDocument)#34 (1) {
        [
          "storage": "ArrayObject":private
        ]=&gt;
        array(3) {
          [
            "name"
          ]=&gt;
          string(14) "Daniel Higgins"
          [
            "email"
          ]=&gt;
          string(17) "d-higgins@mac.com"
          [
            "mobileNo"
          ]=&gt;
          string(13) "(408)555-3514"
        }
      }
      [
        9
      ]=&gt;
      object(MongoDB\Model\BSONDocument)#35 (1) {
        [
          "storage": "ArrayObject":private
        ]=&gt;
        array(3) {
          [
            "name"
          ]=&gt;
          string(14) "John Appleseed"
          [
            "email"
          ]=&gt;
          string(22) "John-Appleseed@mac.com"
          [
            "mobileNo"
          ]=&gt;
          string(12) "888-555-5512"
        }
      }
      [
        10
      ]=&gt;
      object(MongoDB\Model\BSONDocument)#36 (1) {
        [
          "storage": "ArrayObject":private
        ]=&gt;
        array(3) {
          [
            "name"
          ]=&gt;
          string(14) "John Appleseed"
          [
            "email"
          ]=&gt;
          string(22) "John-Appleseed@mac.com"
          [
            "mobileNo"
          ]=&gt;
          string(12) "888-555-1212"
        }
      }
      [
        11
      ]=&gt;
      object(MongoDB\Model\BSONDocument)#37 (1) {
        [
          "storage": "ArrayObject":private
        ]=&gt;
        array(3) {
          [
            "name"
          ]=&gt;
          string(9) "Anna Haro"
          [
            "email"
          ]=&gt;
          string(17) "anna-haro@mac.com"
          [
            "mobileNo"
          ]=&gt;
          string(12) "555-522-8243"
        }
      }
      [
        12
      ]=&gt;
      object(MongoDB\Model\BSONDocument)#38 (1) {
        [
          "storage": "ArrayObject":private
        ]=&gt;
        array(3) {
          [
            "name"
          ]=&gt;
          string(12) "Hank Zakroff"
          [
            "email"
          ]=&gt;
          string(20) "hank-zakroff@mac.com"
          [
            "mobileNo"
          ]=&gt;
          string(13) "(555)766-4823"
        }
      }
      [
        13
      ]=&gt;
      object(MongoDB\Model\BSONDocument)#39 (1) {
        [
          "storage": "ArrayObject":private
        ]=&gt;
        array(3) {
          [
            "name"
          ]=&gt;
          string(12) "Hank Zakroff"
          [
            "email"
          ]=&gt;
          string(20) "hank-zakroff@mac.com"
          [
            "mobileNo"
          ]=&gt;
          string(13) "(707)555-1854"
        }
      }
      [
        14
      ]=&gt;
      object(MongoDB\Model\BSONDocument)#40 (1) {
        [
          "storage": "ArrayObject":private
        ]=&gt;
        array(3) {
          [
            "name"
          ]=&gt;
          string(12) "David Taylor"
          [
            "email"
          ]=&gt;
          string(20) "hank-zakroff@mac.com"
          [
            "mobileNo"
          ]=&gt;
          string(12) "555-610-6679"
        }
      }
    }
  }
  </code></pre>
  
  <p>for unsetting the index i did what i normally do in arrays</p>
  
  <pre><code>foreach ($is_exist['contacts'] as $key =&gt; $value) {
      if(@$value['deleted_at']){
        unset($is_exist['contacts'][$key]);
      }
  }
  </code></pre>
  
  <p>now i have matching objects at key 0 and 1 but the problem is it unsets the key 0 but not key 1 i have tried many combinations like </p>
  
  <p>//pass by reference doesnot work at all</p>
  
  <pre><code>foreach ($is_exist['contacts'] as $key =&gt; &amp;$value) {
      if(@$value['deleted_at']){
          unset($value);
      }
  }
  </code></pre>
  
  <p>i even tried </p>
  
  <pre><code>reset($is_exist['contacts']);
  </code></pre>
  
  <p>just after my unset condition but that doesn't work as well</p>
  
  <p>it's strange , this doesn't happen normally any idea why this is happening? </p>