意外更改-覆盖foreach php内部的对象


unexpected changes - overriding object inside foreach php

我有一个对象形式数据库的数组,如下所示:

array(3) {
  [1]=>
  array(2) {
    [0]=>
    object(stdClass)#99 (3) {
      ["id"]=>
      string(2) "42"
      ["name"]=>
      string(4) "NAME1"
      ["type"]=>
      string(1) "6"
    }
    [1]=>
    object(stdClass)#98 (3) {
      ["id"]=>
      string(3) "146"
      ["name"]=>
      string(3) "STH1"
      ["type"]=>
      string(1) "2"
    }
  }
  [2]=>
  array(2) {
    [0]=>
    object(stdClass)#97 (3) {
      ["id"]=>
      string(2) "422"
      ["name"]=>
      string(4) "NAME2"
      ["type"]=>
      string(1) "3"
    }
    [1]=>
    object(stdClass)#96 (3) {
      ["id"]=>
      string(3) "16"
      ["name"]=>
      string(3) "STH2"
      ["type"]=>
      string(1) "2"
    }
  }
  [3]=>
  array(2) {
    [0]=>
    object(stdClass)#95 (3) {
      ["id"]=>
      string(2) "11"
      ["name"]=>
      string(4) "NAME3"
      ["type"]=>
      string(1) "5"
    }
    [1]=>
    object(stdClass)#94 (3) {
      ["id"]=>
      string(3) "69"
      ["name"]=>
      string(3) "STH3"
      ["type"]=>
      string(1) "3"
    }
  }
}

如果我想将同一个对象添加到下一个数组并更改其类型的值,我会覆盖当前对象。我该怎么修?我的前臂循环如下:

foreach($events as $key => $event){
    foreach($event as $k => $v){
        if($v->type == 6){
            $v->type = "0";
            $events[$key+1][] = $v;
            $v->type = "6";
        }
    }
}

如果我猜你想要实现的是正确的,我会选择这个

foreach($events as $key => $event){
    foreach($event as $k => $v){
        if($v->type == 6){
            $tmp = $v;
            $tmp->type="0";
            $events[$key+1][] = $tmp;
        }
    }
}