/    Sign up×
Community /Pin to ProfileBookmark

SESSION ARRAY Upate

I’m trying to get my multi dimensional array and if an item exists add a variable to the count.

this code just nests the array deeper each time.

“`
$CartArray[] = $_SESSION[‘MySite’];

print “<pre>”;
print_r($CartArray);
print “</pre>”;

foreach ($CartArray as &$CartItem) {
if ($CartItem[0] == $ID){
$newval = ($CartArray[1] + $Cnt);
$CartItem[1] = $newval;
break;
}
}

# If not in array insert

print “<pre>”;
print_r($CartArray);
print “</pre>”;

unset($_SESSION[‘MySite’]);
$_SESSION[‘MySite’] = $CartArray;

exit();
“`

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@kiwisauthorAug 15.2020 — Everytime i run that page my array just gets deep and deeper into itself
Copy linkTweet thisAlerts:
@SempervivumAug 15.2020 — Shouldn't this be cart**Item**:

`$newval = ($CartItem[1] + $Cnt);`
Copy linkTweet thisAlerts:
@SempervivumAug 15.2020 — BTW: This could be simplified by using an associative array, as mentioned in your other thread:
id (isset($CartArray[$ID])) {
$CartArray[$ID] += $cnt;
} else {
$CartArray[$ID] = $cnt;
}
No loop needed.
Copy linkTweet thisAlerts:
@VITSUSAAug 17.2020 — @kiwis80#1622154 I agree with Sempervivum, this could be simplified by using an associative array as he have mentioned. I hope you got the solution :)
×

Success!

Help @kiwis spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.7,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...