/    Sign up×
Community /Pin to ProfileBookmark

Need help with Multi-dimensional Session Array

Hi,

I am creating a SESSION based array that will contain the items / quantities that a person can purchase on a given day. If the person has a userid of say 171, and purchases items through a form, then the SESSION array will be populated as:

$_SESSION[171][0]= array(‘dateval’=>’2012-07-29’,’item1#’=>12,’quantity1’=>2,’item2#’=>16,’quantity2’=>1,’item3#’=>16,’quantity3’=>1))
$_
SESSION[171][1]= array(‘dateval’=>’2012-07-30’,’item1#’=>4,’quantity1’=>1,’item2#’=>20,’quantity2’=>2,’item3#’=>16,’quantity3’=>2))

Now through the same form the person may go and modify one of the quantities, say they change the quantity (represented by quantity2), for item2# on 2012-07-30. The change is that instead of 2 units of item2# they choose to buy 1.

So the new values for that day will be:
‘dateval’=>’2012-07-30’,’item1#’=>4,’quantity1’=>1,’item2#’=>20,’quantity2’=>1,’item3#’=>16,’quantity3’=>2

I want to know what I can use to search if an entry for 2012-07-30 already exists in the $_SESSION array and if it exists delete the entire entry corresponding to 2012-07-30 from the $_SESSION array so I can insert this new row for that day instead.

There are so many array functions but I am not sure which one will fit my need.

Will it be easier to do a replacement of values for that day’s entries instead. If so how?

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@sansenJul 30.2012 — If I understood you Then

[code=php]$_SESSION[171][1]['quantity2']= $newValue ;[/code]

This will change the old value to the new value
Copy linkTweet thisAlerts:
@BelrickJul 30.2012 — The way you are organising your array is making life hard for you.

Seriously recommend you change your structure.

[code=php]
if(isset($_SESSION[$userid][$todaysdate])){
unset($_SESSION[$userid][$todaysdate]);
$_SESSION[$userid][$todaysdate] = array('item1#'=>4,'quantity1'=>1,'item2#'=>20,'quantity2'=>2,'item3#'=>16,'quantity3'=>2));

}
[/code]


Dont need to unset but there to show you it being deleted.

The key to multi dimensional arrays IMHO is to not only use associative arrays but clever indexing that helps with searching and organising.
×

Success!

Help @rcr 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.28,
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,
)...