简体   繁体   English

从对象数组中删除对象的PHP?

[英]removing objects from an object array in php?

for example say i have an object array called $list looks like this: 例如说我有一个名为$ list的对象数组,如下所示:

<(stdClass)#16 (3) {
  ["1"]=>
  <(8) "50504496"
  ["2"]=>
  <(8) "12435374"
  ["3"]=>
  <(8) "12436374"

Im doing a foreach on the object array and removing them if they exist in the database ie 我在对象数组上做一个foreach,如果它们存在于数据库中则将它们删除

   foreach($list as $l){

    //do the query

    if( it exists){
      //remove from objects: this is where i need help!!
      }
   }

i have the db logic, im just stuck to know how i can remove objects, i was thinking maybe i should create a new object and add them thier. 我有数据库逻辑,我只是​​想知道我如何删除对象,我在想也许我应该创建一个新对象并添加它们。 thanks } 谢谢 }

Use: unset 使用:未设置

foreach($list as $key => $obj){
  if( exists .. ) {
      unset($list[$key]);
  }
}

运行查询,如果返回的行数>0 ,那么您有结果,请使用unset($object->$var)取消设置变量

Try like: 试试:

    foreach($list as $li)
    {
        if($li)
        {
            //copy into another array.
        }
    }

i thnk its may simple 我认为这可能很简单

Try like: $i=0; 试试:$ i = 0;

  foreach($list as $li)
  {
      if($li)
      {
          $b[i] = $li;
          $i++;
      }
   }

array '$b' will hold the values in $list which are not null 数组'$ b'将保存$ list中不为null的值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM