Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jan 2014
    Posts
    74
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Updating an array key value

    Hi all,

    I have made the following variable with arrays:

    PHP Code:
    $_SESSION['cart']['content'] = array(
        array(
    number => 1sizes => 1)
        ,
        array(
    number => 2sizes => 1)
    ); 
    Can someone please tell me how I can update the key 'sizes' where 'number = 2' without it touching anything else?

  • #2
    New Coder
    Join Date
    Jan 2014
    Posts
    74
    Thanks
    11
    Thanked 0 Times in 0 Posts
    To further explain on what I am trying to do, in MySQL I would do the equivilent of something along the lines of:

    Code:
    UPDATE content SET sizes = 2 WHERE number = 2;
    I'm basically wanting to do the same, but for an array in a PHP session.

    Perhaps I have engineered my session and array wrong for a cart system?

  • #3
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    721
    Thanks
    20
    Thanked 84 Times in 84 Posts
    your session var is the same as:

    PHP Code:
    $_SESSION['cart']['content'][0]['number'];//1
    $_SESSION['cart']['content'][0]['sizes'];//1
    $_SESSION['cart']['content'][1]['number'];//2
    $_SESSION['cart']['content'][1]['sizes'];//1 
    so you would need to alter like this:

    PHP Code:

    $_SESSION
    ['cart']['content'][1]['sizes'] = '5'
    i think it would be easier to put size inside the number array and drop the unassinged array after content

    something like

    PHP Code:

    $_SESSION
    ['cart']['content']['number'][1]['sizes'] = '1'
    $_SESSION['cart']['content']['number'][2]['sizes'] = '1'
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • #4
    Regular Coder
    Join Date
    Oct 2004
    Posts
    363
    Thanks
    0
    Thanked 18 Times in 18 Posts
    Loop through them
    PHP Code:
    foreach($_SESSION['cart']['content'] as $content_k => $content_v)
    {
        if(
    $content_v['number'] == 1)
        {
            
    $_SESSION['cart']['content'][$content_k]['sizes'] = 'new_value';
        }



  •  

    Posting Permissions

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