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 6 of 6
Like Tree1Likes
  • 1 Post By sunfighter

Thread: foreach with session variables containing an array

  1. #1
    New Coder
    Join Date
    Jan 2014
    Posts
    74
    Thanks
    11
    Thanked 0 Times in 0 Posts

    foreach with session variables containing an array

    Hi all,

    I have multiple session varriables:

    $_SESSION['cart']['content']['product1']
    $_SESSION['cart']['content']['product2']
    $_SESSION['cart']['content']['product3']


    These have array data in them, such as (productnumber = 1, size = 3)

    How can I do a foreach for each of these variables and then using the array data within? Do I do?:

    PHP Code:
    foreach ($_SESSION['cart']['content'] as $content) {
        echo 
    $content['productnumber'];
        echo 
    $content['size'];


  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,694
    Thanks
    25
    Thanked 657 Times in 656 Posts
    Just a thought. Maybe it would be better to set your $_SESSION up with the product name in the content array?
    LOOK:
    PHP Code:
    <?php
    $shop
    ['cart']['content'] = array(
        array(
    'product' =>"belt"'productnumber' => 12505 'size' => 3),
        array(
    'product' =>"pants"'productnumber' =>75784 'size' =>25),
        array(
    'product' =>"shirt"'productnumber' =>11568 'size' =>7'color'=> 'red')
    );

    foreach(
    $shop['cart']['content'] as $item){
        echo 
    $item['product'] . '<br>';
        echo 
    $item['productnumber'] . '<br>';
        echo 
    $item['size'] . '<br>';
        if(
    array_key_exists("color"$item))
            echo 
    $item["color"] . '<br>';
    }
    ?>
    I added something in the 'product' =>"shirt" array to show you how to do that.

    OR you could do this:
    PHP Code:
    foreach($shop['cart']['content'] as $item){
        foreach(
    $item as $key => $value){
            echo 
    "$key : $value<br>";
        }echo 
    "-----------<br>";

    Last edited by sunfighter; 05-05-2014 at 04:19 PM.
    Phaelon likes this.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • Users who have thanked sunfighter for this post:

    Phaelon (05-06-2014)

  • #3
    New Coder
    Join Date
    Jan 2014
    Posts
    74
    Thanks
    11
    Thanked 0 Times in 0 Posts
    Thanks starfighter. You are always helpful.

    I am going to use the method in your first example. Its perfect.

    Thanks for the alternative method aswell.


    Enjoy your day!

  • #4
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,694
    Thanks
    25
    Thanked 657 Times in 656 Posts
    Thank you Phaelon, that was nice of you to say.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #5
    New Coder
    Join Date
    Jan 2014
    Posts
    74
    Thanks
    11
    Thanked 0 Times in 0 Posts
    I am having trouble understanding the dynamics of sessions and their keys and wondering if someone could please explain to me how to do what I want with my existing code. I've tried for 45 minutes and can't get it working. Frustrating!! :S

    This is the dynamics in a visualisation of what I am trying to do:

    $_SESSION['cart']['content'](number = 1)(sizes = 1)
    $_SESSION['cart']['content'](number = 2)(sizem = 3)


    This is my attempt at it:
    PHP Code:
    if (isset($_POST['cartquantity'])) {
        if (empty(
    $_SESSION['cart']['content']['number'][''.$_POST['number'].''])) {
            
    $_SESSION['cart']['content'] = array('number' => $_POST['number']);
        }
        if (
    $_POST['cartsize'] == 'S') {
            if (isset(
    $_SESSION['cart']['content']['number'][''.$_POST['number'].'']['sizes'])) {
                
    $_SESSION['cart']['content']['number'][''.$_POST['number'].'']['sizes'] = $_SESSION['cart']['content']['number'][''.$_POST['number'].'']['sizes'] + $_POST['cartquantity'];
            } else {
                
    $_SESSION['cart']['content']['number'][''.$_POST['number'].''] = array('sizes' => $_POST['cartquantity']);
            }
        }
        if (
    $_POST['cartsize'] == 'M') {
            if (isset(
    $_SESSION['cart']['content']['number'][''.$_POST['number'].'']['sizem'])) {
                
    $_SESSION['cart']['content']['number'][''.$_POST['number'].'']['sizem'] = $_SESSION['cart']['content']['number'][''.$_POST['number'].'']['sizem'] + $_POST['cartquantity'];
            } else {
                
    $_SESSION['cart']['content']['number'][''.$_POST['number'].''] = array('sizem' => $_POST['cartquantity']);
            }
        }


  • #6
    New Coder
    Join Date
    Jan 2014
    Posts
    74
    Thanks
    11
    Thanked 0 Times in 0 Posts
    solved it on the last attempt before i was about to storm out and go for a drive lol

    Ok, so the trick is to create the parents variable first before defining a new one:

    PHP Code:
            if (empty($_SESSION['cart']['content'][''.$_POST['number'].''])) {
                isset(
    $_SESSION['cart']['content'][''.$_POST['number'].'']);
            }

            if (
    $_POST['cartsize'] == 'S') {
                
    $_SESSION['cart']['content'][''.$_POST['number'].'']['sizes'] = $_SESSION['cart']['content'][''.$_POST['number'].'']['sizes'] + $_POST['cartquantity'];
            }
            if (
    $_POST['cartsize'] == 'M') {
                
    $_SESSION['cart']['content'][''.$_POST['number'].'']['sizem'] = $_SESSION['cart']['content'][''.$_POST['number'].'']['sizem'] + $_POST['cartquantity'];
            } 


  •  

    Posting Permissions

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