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?