0

I've got array of object form database like below:

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

And if i want to add the same object to the next array and change value of its type, i override the current object. How can i fix it? My foreach loop below:

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

1 Answers1

0

If my guess what you are trying to achieve is right i would go like this

foreach($events as $key => $event){
    foreach($event as $k => $v){
        if($v->type == 6){
            $tmp = $v;
            $tmp->type="0";
            $events[$key+1][] = $tmp;
        }
    }
}
StormRideR
  • 1,738
  • 1
  • 14
  • 13
  • any changes - it override the first object too! – jdoe Jul 28 '16 at 07:43
  • @jdoe So i think that your problem is somewhere else, first gues is having several objects with same id. if you are indexing column with events by id, than secound instance with same id and different type will just update old entry – StormRideR Jul 28 '16 at 07:51
  • i've already found solution: http://stackoverflow.com/a/185939/6648280 – jdoe Jul 28 '16 at 08:18