PHP防止foreach循环覆盖对象 [英] PHP prevent foreach loop from overwriting object

查看:179
本文介绍了PHP防止foreach循环覆盖对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第四次尝试编写此问题,所以请多多包涵。

This is the fourth time I've tried writing this question, so please bear with me.

我有一个来自数据库查询的PHP对象,该查询将返回以下数据:

I have a PHP object that comes from a DB Query which pulls back the following data:

[1] => stdClass Object
    (
        [eventId] => 11
        [eventName] => Second Event
        [...]
        [eventChildren] => Array
            (
                [0] => stdClass Object
                    (
                        [childId] => 8
                        [childName] => Jane Doe
                        [...]
                        [gifts] => Array
                            (
                                [0] => stdClass Object
                                    (
                                        [giftId] => 73
                                        [giftName] => My two front teeth
                                        [childId] => 8
                                        [userId] => 1
                                        [eventId] => 11
                                    )
                                [1] => stdClass Object
                                    (
                                        [giftId] => 74
                                        [giftName] => Wasps
                                        [childId] => 8
                                        [userId] => 1
                                        [eventId] => 11
                                    )

                            )

                    )

            )

    )
)

我正在运行一系列 foreach 循环以比较 gifts 数组中的 userId userId 存储在会话cookie中。

I'm then running a massive series of foreach loops in order to compare the userId from the gifts array against the userId stored in the session cookie.

从这些循环中,我创建了一系列由用户组成的孩子和礼物已经选择了。

From these loops I'm creating an array of children and gifts that the user has selected.

问题是这会覆盖我的主要对象,而不是创建一个新对象。

The problem is this overwrites my main object rather than creating a new one.

循环:

$user = $this->session->userdata('user');
$tempEvents = $events;
$userSelection = array();
$flag = FALSE;

foreach ( $tempEvents as $i => $event )
{
    if ( $i == 0 )
    {
        foreach ( $event->eventChildren as $child ) 
        {
            $userGift = array();

            foreach ( $child->gifts as $gift )
            {
                if ( $gift->userId == $user['userId'] )
                {
                    array_push($userGift, $gift);
                    $flag = TRUE;
                }
            }

            $tempChild = $child;
            $tempChild->gifts = $userGift;

            if ( $flag )
            {
                array_push($userSelection, $tempChild);
                $flag = FALSE;
            }
        }
    }
}

如果我 print_r($ events); 它显示了已编辑的列表,而不是事件的完整列表。有没有一种方法可以创建重复的对象并对其进行编辑,而不是编辑原始对象?

If I print_r($events); it displays the edited list rather than it's full list of events. Is there a way to create a duplicate object and edit that rather than editing the original object?

推荐答案

覆盖的原因是 $ tempChild = $ child;

那不会深度复制 $ child ,但使 $ tempChild $ child 指向相同的数据结构

That will not deep copy the contents of $child but make both $tempChild and $child point towards the same data structure, which obviously isn't desirable in this case.

您应该使用 clone ,如下例所示。

You should use clone as in the below example.

$tempChild = clone $child;







  • PHP:对象克隆-手册

  • 这篇关于PHP防止foreach循环覆盖对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆