我有一些对象形式数据库,如下所示:
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循环如下:
foreach($events as $key => $event){
foreach($event as $k => $v){
if($v->type == 6){
$v->type = "0";
$events[$key+1][] = $v;
$v->type = "6";
}
}
}
答案 0 :(得分:0)
如果我猜你想要达到的目标是正确的,我会这样做
foreach($events as $key => $event){
foreach($event as $k => $v){
if($v->type == 6){
$tmp = $v;
$tmp->type="0";
$events[$key+1][] = $tmp;
}
}
}