3

So I'm trying to use a foreach loop to echo out all the values of an array in my $_SESSION array as follows.

 <?php
      foreach($_SESSION['movie'] as $key => $value){
          echo "Movie: " . $key;
      }
      foreach($_SESSION['session'] as $key => $value){
          echo "Session: " . $key;
      }
?>

I'm pretty new to PHP so don't have much idea if I'm even on the right track.

This is the var_dump of the $_SESSION array.

array(1) { 
    ["cart"]=> array(2) { 
        ["ACWED-09"]=> array(3) { 
            ["movie"]=> string(2) "AC" 
            ["session"]=> string(6) "WED-09" 
            ["seats"]=> array(1) { ["SF"]=> int(7) }
        }   
        ["ACFRI-09"]=> array(3) { 
            ["movie"]=> string(2) "AC" 
            ["session"]=> string(6) "FRI-09" 
            ["seats"]=> array(1) { ["SF"]=> int(2) } 
        } 
    } 
} 
6
  • What's in the session array. Do a var_dump Commented Oct 14, 2017 at 6:29
  • why you use $key? Commented Oct 14, 2017 at 6:29
  • can you show us whole session array so we can see what are values are in session. Commented Oct 14, 2017 at 6:31
  • array(1) { ["cart"]=> array(2) { ["ACWED-09"]=> array(3) { ["movie"]=> string(2) "AC" ["session"]=> string(6) "WED-09" ["seats"]=> array(1) { ["SF"]=> int(7) } } ["ACFRI-09"]=> array(3) { ["movie"]=> string(2) "AC" ["session"]=> string(6) "FRI-09" ["seats"]=> array(1) { ["SF"]=> int(2) } } } } Commented Oct 14, 2017 at 6:42
  • You don't have $_SESSION['movie'] there. You have $_SESSION['cart']['ACWED-09']['movie'] and it contains the string "AC", not an array. Commented Oct 14, 2017 at 6:46

5 Answers 5

1

The movie is nested in the $_SESSION['cart'] array. You need to do:

foreach ($_SESSION['cart'] as $key => $item) {
    echo "Key: $key<br>";
    if (isset($item['movie']) {
        echo "Movie: {$item['movie']}<br>";
    }
    if (isset($item['session']) {
        echo "Session: {$item['session']}<br>";
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

So that prints but I still get errors saying undefined index Key: WED-09 Movie: AC Notice: Undefined index: session in /home/sh5/S3663675/public_html/wp/a3/showing.php on line 107 Session: Key: ACWED-09 Notice: Undefined index: movie in /home/sh5/S3663675/public_html/wp/a3/showing.php on line 106 Movie: Session: WED-09
Are there items in your cart that don't have a movie or session?
You can test whether those keys exist in the item with isset().
Checked with that figured out I had accidentally split the movie array, thanks for all the help.
1

You should try this..

foreach($_SESSION['movie'] as $key => $value) {
echo  'Movie ' . $value . '<br/>';
}
echo '<hr/>';
foreach($_SESSION['session'] as $key => $value) {
echo  'Session' . $value . '<br/>';
}

Comments

1

An array pairs values to keys, as stated here. Keys are the indexes in the array.

foreach($_SESSION['movie'] as $key => $value){
    echo "At the index " . $key . ", the movie is " . $value;
}

So you should go with echo "Movie: " . $value; if you want to write the value. The key isn't relevant here.

Note that you don't have to use the "$key => $value" pairing if you don't care about the indexes within your loop. This works too :

foreach($_SESSION['movie'] as $value){
    echo "Movie: " . $value;
}

You don't have access to the value's index in that case.

3 Comments

Trying this gave me this error. Notice: Undefined index: movie in /home/sh5/S3663675/public_html/wp/a3/showing.php on line 104 Warning: Invalid argument supplied for foreach() in /home/sh5/S3663675/public_html/wp/a3/showing.php on line 104
@Joshm102 If you're getting that error, it means you don't have a session variable named movie. Did you remember to put session_start(); at the beginning of the script?
Just posted a var_dump and it says the $_SESSION['movie'] has a value in it
0
  foreach($_SESSION['movie'] as $key => $value){
      echo "Movie: " .$key."=". $value;
  }
  foreach($_SESSION['session'] as $key => $value){
      echo "Session: " .$key."=". $value;
  }

Here is an example for associative array what is key and what is value

$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");

foreach($age as $x => $x_value) {
    echo "Key=" . $x . ", Value=" . $x_value;
    echo "<br>";
}

1 Comment

You need to clarify things from the OP what the session array has before guessing
0
echo '<pre>';
print_r($_SESSION);
echo '</pre>';

foreach ($_SESSION['cart'] as $key => $item) {
    echo "Key: $key<br>";
    echo "Movie: {$item['movie']}<br>";
    echo "Session: {$item['session']}<br>";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.