PHP prevent foreach loop from overwriting object

Deadly 提交于 2020-01-16 04:23:05

问题


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

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
                                    )

                            )

                    )

            )

    )
)

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.

The Loops:

$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;
            }
        }
    }
}

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?


回答1:


The reason for the "overwriting" is $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.

You should use clone as in the below example.

$tempChild = clone $child;

  • PHP: Object Cloning - Manual



回答2:


Try

$tempEvents = clone $events;


来源:https://stackoverflow.com/questions/8542682/php-prevent-foreach-loop-from-overwriting-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!