1

so long story short - unsetting works fine when trying to unset $this->models[$modelKey] in first foreach, $this->models[$modelKey]->equipmentList->equipment[$eqKey] points to the correct object, but unsetting it in nested foreach doesn't work. Any ideas? Thanks in advance for help.

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]);
          }
        }
      }
    }
  }
Anthony
  • 36,459
  • 25
  • 97
  • 163

2 Answers2

0

isn't it accessed via $model and not $this->models? And with $equipment the same.

Just try this:

unset($equipment[$eqKey]); 

Some expansion:

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]
    } 
} 

EDIT:

Now I got the solution:

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]);
    } 
} 
WolvDev
  • 3,182
  • 1
  • 17
  • 32
  • Tried: unset($equipment);, unset($model->equipmentList->equipment[$eqKey]); - both solutions won't work ($equipment is an object so $equipment[$eqKey] wouldn't be accurate). Also using reference to those objects - doesn't change a thing when doing it on this: foreach ($model->equipmentList->equipment as $eqKey => &$equipment), running out of memory when doing it on foreach ($this->models as $modelKey => &$model) – Maciej Jaśniaczyk Apr 20 '12 at 12:48
  • Do you want to unset the object or a property of the object? – WolvDev Apr 20 '12 at 12:51
  • I'm trying to unset object (which is kept in array). To clarify: $this->models - Array, $model->equipmentList - object, $model->equipmentList->equipment - Array of objects – Maciej Jaśniaczyk Apr 20 '12 at 12:52
  • you wrote in your first comment that you tried unset($equipment); (the array of objects) but not unset($equipment[$eqKey]); (the object) – WolvDev Apr 20 '12 at 13:14
  • foreach ($model->equipmentList->equipment as $eqKey => $equipment) - $equipment is an object, $model->equipmentList->equipment is an array. Bad naming from me. :P – Maciej Jaśniaczyk Apr 20 '12 at 13:16
  • You wrote that `$this->models[$modelKey]->equipmentList->equipment[$eqKey]` points to the right object, so `$equipment` point there, too. Right? Have you tried to set it to null? Or do you want to remove it from the Array, too? – WolvDev Apr 20 '12 at 13:29
  • 1. Exactly. $this->models[$modelKey]->equipmentList->equipment[$eqKey] is the same object as $equipment. Need to remove it from array. Will check if setting to null works though. – Maciej Jaśniaczyk Apr 20 '12 at 13:33
  • $this->models[$modelKey]->equipmentList->equipment[$eqKey]=null; var_dump($this->models[$modelKey]->equipmentList->equipment[$eqKey]);die(); and i still get object :/ – Maciej Jaśniaczyk Apr 20 '12 at 13:38
  • Think you meant $model->equipmentList->equipment[$eqKey] ($modelKey is a key of different array). Anyway - still doesn't work (i also think it should!!! :P) – Maciej Jaśniaczyk Apr 20 '12 at 13:50
  • no, I meant $modelKey and not $eqkey. cause you tested $eqKey already. Give it a try with $modelKey. – WolvDev Apr 20 '12 at 13:51
  • I think I'm toooo confused now O_o – WolvDev Apr 20 '12 at 13:57
  • Tried it as well. Still no luck – Maciej Jaśniaczyk Apr 20 '12 at 13:57
  • me too, sitting watching at this and trying different aproaches for like 2 hrs, and still no luck. – Maciej Jaśniaczyk Apr 20 '12 at 14:00
  • Finally succeed :) added remove($eqKey) function to equipmentList class and it worked that way. – Maciej Jaśniaczyk Apr 20 '12 at 14:45
  • hmmm, I had this idea 2 hours ago, but I don't know why I didn't posted it -.- my first lecture in programming was: the elephant comes with his own hunt-method :D (means each class has it's update-method, something from C#) – WolvDev Apr 20 '12 at 16:53
0

In case I'm spot on, I'll make this an answer, but if I'm wrong, I'll move this to a comment:

First, you clone the object you are trying to unset at the end. Why not just unset it right after the clone?

Second, I think that object is passed by reference to the foreach loop by design, so you don't need to reference the key.

If I'm right, this might work:

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);
          }
        }
      }
    }
  }
Anthony
  • 36,459
  • 25
  • 97
  • 163
  • 1. I'm trying to unset $equipment, not $model. 2. idea about references came from here (http://stackoverflow.com/questions/2304570/how-to-delete-object-from-array-inside-foreach-loop). They say i need to use it if want to unset that way. 3. Moving unset after $newEquipmentList->add(clone $equipment); didn't change anything. EDIT: pasted wrong link. Now it's correct – Maciej Jaśniaczyk Apr 20 '12 at 13:09