简体   繁体   中英

PHP prevent foreach loop from overwriting object

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?

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;

尝试

$tempEvents = clone $events;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM