www.webdeveloper.com
+ Reply to Thread
Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2012
    Posts
    3

    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?

  2. #2
    Join Date
    Sep 2008
    Posts
    17
    If I understood you Then

    PHP Code:
    $_SESSION[171][1]['quantity2']= $newValue 
    This will change the old value to the new value

  3. #3
    Join Date
    Apr 2010
    Posts
    223
    The way you are organising your array is making life hard for you.

    Seriously recommend you change your structure.

    PHP Code:
    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));


    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.
    Last edited by Belrick; 07-30-2012 at 04:57 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center



Recent Articles