首页 > 解决方案 > php unset 不适用于对象数组

问题描述

我有一个从 mongodb 接收的对象数组我有一个自定义软删除功能,为此我必须循环遍历对象找到密钥deleted_at并将其从数组中取消设置如果我转储它,这就是我的数组的样子

object(MongoDB\Model\BSONArray)#41 (1) {
  [
    "storage": "ArrayObject":private
  ]=>
  array(15) {
    [
      0
    ]=>
    object(MongoDB\Model\BSONDocument)#26 (1) {
      [
        "storage": "ArrayObject":private
      ]=>
      array(4) {
        [
          "name"
        ]=>
        string(18) "BCasdaadaadadasdas"
        [
          "email"
        ]=>
        string(20) "hank-zakroff@mac.com"
        [
          "mobileNo"
        ]=>
        string(11) "11-22-33-44"
        [
          "deleted_at"
        ]=>
        string(19) "2019-08-21 10:08:43"
      }
    }
    [
      1
    ]=>
    object(MongoDB\Model\BSONDocument)#27 (1) {
      [
        "storage": "ArrayObject":private
      ]=>
      array(4) {
        [
          "name"
        ]=>
        string(9) "ADasdBSDS"
        [
          "email"
        ]=>
        string(23) "haasdnk-zakroff@mac.com"
        [
          "mobileNo"
        ]=>
        string(11) "03338142535"
        [
          "deleted_at"
        ]=>
        string(19) "2019-08-21 11:08:17"
      }
    }
    [
      2
    ]=>
    object(MongoDB\Model\BSONDocument)#28 (1) {
      [
        "storage": "ArrayObject":private
      ]=>
      array(3) {
        [
          "name"
        ]=>
        string(12) "Hank Zakroff"
        [
          "email"
        ]=>
        string(20) "hank-zakroff@mac.com"
        [
          "mobileNo"
        ]=>
        string(11) "03244424280"
      }
    }
    [
      3
    ]=>
    object(MongoDB\Model\BSONDocument)#29 (1) {
      [
        "storage": "ArrayObject":private
      ]=>
      array(3) {
        [
          "name"
        ]=>
        string(12) "Hank Zakroff"
        [
          "email"
        ]=>
        string(20) "hank-zakroff@mac.com"
        [
          "mobileNo"
        ]=>
        string(11) "03244424281"
      }
    }
    [
      4
    ]=>
    object(MongoDB\Model\BSONDocument)#30 (1) {
      [
        "storage": "ArrayObject":private
      ]=>
      array(3) {
        [
          "name"
        ]=>
        string(9) "Kate Bell"
        [
          "email"
        ]=>
        string(17) "kate-bell@mac.com"
        [
          "mobileNo"
        ]=>
        string(13) "(555)564-8583"
      }
    }
    [
      5
    ]=>
    object(MongoDB\Model\BSONDocument)#31 (1) {
      [
        "storage": "ArrayObject":private
      ]=>
      array(3) {
        [
          "name"
        ]=>
        string(9) "Kate Bell"
        [
          "email"
        ]=>
        string(17) "kate-bell@mac.com"
        [
          "mobileNo"
        ]=>
        string(13) "(415)555-3695"
      }
    }
    [
      6
    ]=>
    object(MongoDB\Model\BSONDocument)#32 (1) {
      [
        "storage": "ArrayObject":private
      ]=>
      array(3) {
        [
          "name"
        ]=>
        string(14) "Daniel Higgins"
        [
          "email"
        ]=>
        string(17) "d-higgins@mac.com"
        [
          "mobileNo"
        ]=>
        string(12) "555-478-7672"
      }
    }
    [
      7
    ]=>
    object(MongoDB\Model\BSONDocument)#33 (1) {
      [
        "storage": "ArrayObject":private
      ]=>
      array(3) {
        [
          "name"
        ]=>
        string(14) "Daniel Higgins"
        [
          "email"
        ]=>
        string(17) "d-higgins@mac.com"
        [
          "mobileNo"
        ]=>
        string(13) "(408)555-5270"
      }
    }
    [
      8
    ]=>
    object(MongoDB\Model\BSONDocument)#34 (1) {
      [
        "storage": "ArrayObject":private
      ]=>
      array(3) {
        [
          "name"
        ]=>
        string(14) "Daniel Higgins"
        [
          "email"
        ]=>
        string(17) "d-higgins@mac.com"
        [
          "mobileNo"
        ]=>
        string(13) "(408)555-3514"
      }
    }
    [
      9
    ]=>
    object(MongoDB\Model\BSONDocument)#35 (1) {
      [
        "storage": "ArrayObject":private
      ]=>
      array(3) {
        [
          "name"
        ]=>
        string(14) "John Appleseed"
        [
          "email"
        ]=>
        string(22) "John-Appleseed@mac.com"
        [
          "mobileNo"
        ]=>
        string(12) "888-555-5512"
      }
    }
    [
      10
    ]=>
    object(MongoDB\Model\BSONDocument)#36 (1) {
      [
        "storage": "ArrayObject":private
      ]=>
      array(3) {
        [
          "name"
        ]=>
        string(14) "John Appleseed"
        [
          "email"
        ]=>
        string(22) "John-Appleseed@mac.com"
        [
          "mobileNo"
        ]=>
        string(12) "888-555-1212"
      }
    }
    [
      11
    ]=>
    object(MongoDB\Model\BSONDocument)#37 (1) {
      [
        "storage": "ArrayObject":private
      ]=>
      array(3) {
        [
          "name"
        ]=>
        string(9) "Anna Haro"
        [
          "email"
        ]=>
        string(17) "anna-haro@mac.com"
        [
          "mobileNo"
        ]=>
        string(12) "555-522-8243"
      }
    }
    [
      12
    ]=>
    object(MongoDB\Model\BSONDocument)#38 (1) {
      [
        "storage": "ArrayObject":private
      ]=>
      array(3) {
        [
          "name"
        ]=>
        string(12) "Hank Zakroff"
        [
          "email"
        ]=>
        string(20) "hank-zakroff@mac.com"
        [
          "mobileNo"
        ]=>
        string(13) "(555)766-4823"
      }
    }
    [
      13
    ]=>
    object(MongoDB\Model\BSONDocument)#39 (1) {
      [
        "storage": "ArrayObject":private
      ]=>
      array(3) {
        [
          "name"
        ]=>
        string(12) "Hank Zakroff"
        [
          "email"
        ]=>
        string(20) "hank-zakroff@mac.com"
        [
          "mobileNo"
        ]=>
        string(13) "(707)555-1854"
      }
    }
    [
      14
    ]=>
    object(MongoDB\Model\BSONDocument)#40 (1) {
      [
        "storage": "ArrayObject":private
      ]=>
      array(3) {
        [
          "name"
        ]=>
        string(12) "David Taylor"
        [
          "email"
        ]=>
        string(20) "hank-zakroff@mac.com"
        [
          "mobileNo"
        ]=>
        string(12) "555-610-6679"
      }
    }
  }
}

为了取消设置索引,我做了我通常在数组中做的事情

foreach ($is_exist['contacts'] as $key => $value) {
    if(@$value['deleted_at']){
      unset($is_exist['contacts'][$key]);
    }
}

现在我在键 0 和 1 有匹配的对象,但问题是它取消了键 0 但不是键 1 我尝试了很多组合,比如

//通过引用传递根本不起作用

foreach ($is_exist['contacts'] as $key => &$value) {
    if(@$value['deleted_at']){
        unset($value);
    }
}

我什至试过

reset($is_exist['contacts']);

就在我未设置的条件之后,但这也不起作用

很奇怪,这通常不会发生,知道为什么会这样吗?

标签: php

解决方案


...问题是它取消了密钥 0 但不是密钥 1

在迭代其项目时​​就地改变数组通常不是一个好习惯。

您一定会遇到这个问题,因为内部数组指针(PHP 版本 < 7)在unset操作中被破坏。

您可以制作原始数组的副本并在修改原始数组时迭代该副本,或者更好地使用未删除的文档制作过滤副本。我更喜欢后一种方法,以直截了当且清楚地表达意图。例如

$contacts = $is_exist['contacts'];
$active_contacts = [];
foreach ($contacts as $key => &$value) {
    if(is_null(@$value['deleted_at']))
    {
      $active_contacts[] = @$value;
    }
}
$is_exist['contacts'] = $active_contacts;

推荐阅读