PHP - 在嵌套的foreach循环中取消设置对象

时间:2012-04-20 12:39:28

标签: php arrays oop foreach unset

如此长的故事简短 - 在第一个foreach中尝试取消设置$this->models[$modelKey]时,取消设置正常,$this->models[$modelKey]->equipmentList->equipment[$eqKey]指向正确的对象,但在嵌套的foreach中取消设置不起作用。有任何想法吗?在此先感谢您的帮助。

public function processModelsEquipmentList() {
    foreach ($this->models as $modelKey => $model) {
      if (!strstr('#', $model->id)) {
        foreach ($model->equipmentList->equipment as $eqKey => $equipment) {
          if (in_array($equipment->code, $this->specialVersionsCodes)) {
            $newModel = clone $model;   
            $newModel->name.= ' ' . $equipment->name;
            $newModel->id.= '#' . $equipment->id;
            if (strlen($newModel->code) < 4) {
              $newModel->code.=$equipment->code;
            }
            $newModel->order = $newModel->order + 1;
            $newEquipmentList = new EquipmentList($newModel->id, true);
            $newEquipmentList->add(clone $equipment);
            $newModel->setNewEquipmentList($newEquipmentList);
            $this->addModel($newModel);
            //echo $this->models[$modelKey]->equipmentList->equipment[$eqKey]->name;die();
            unset($this->models[$modelKey]->equipmentList->equipment[$eqKey]);
          }
        }
      }
    }
  }

2 个答案:

答案 0 :(得分:0)

是不是通过$ model访问而不是$ this-&gt;模型?与$设备相同。

试试这个:

unset($equipment[$eqKey]); 

一些扩展:

foreach ($this->models as $modelKey => $model) {
    //here you use $model to access $this->models[X]
    foreach ($model->equipmentList->equipment as $eqKey => $equipment) { 
        //here you use $equipment to access $this->models[X]->equipmentList->equipment[x]
    } 
} 

修改

现在我得到了解决方案:

foreach ($this->models as $modelKey => $model) {
    foreach ($model->equipmentList->equipment as $eqKey => $equipment) {
        //use the $modelKey key and not the $eqKey
        unset($model->equipmentList->equipment[$modelKey]);
    } 
} 

答案 1 :(得分:0)

如果我发现了,我会回答这个问题,但如果我错了,我会将其发表评论:

首先,克隆您最后要设置的对象。为什么不在克隆之后立即取消它?

其次,我认为该对象是通过设计引用传递给foreach循环传递的,因此您不需要引用该键。

如果我是对的,这可能有效:

public function processModelsEquipmentList() {
    foreach ($this->models as $modelKey => $model) {
      if (!strstr('#', $model->id)) {
        foreach ($model->equipmentList->equipment as $eqKey => $equipment) {
          if (in_array($equipment->code, $this->specialVersionsCodes)) {
            $newModel = clone $model;
            unset($model);  // <-- Unesetting after clone, using loop var name.
            $newModel->name.= ' ' . $equipment->name;
            $newModel->id.= '#' . $equipment->id;
            if (strlen($newModel->code) < 4) {
              $newModel->code.=$equipment->code;
            }
            $newModel->order = $newModel->order + 1;
            $newEquipmentList = new EquipmentList($newModel->id, true);
            $newEquipmentList->add(clone $equipment);
            $newModel->setNewEquipmentList($newEquipmentList);
            $this->addModel($newModel);
          }
        }
      }
    }
  }