eutu9 Posted December 17, 2007 Share Posted December 17, 2007 I am trying to insert an array into a PHP session I tried this way: $products[0]="product 0"; $products[1]="product 1"; $products[2]="product 2"; $_SESSION['products']=$products; I tried this way: $_SESSION['products'][0]="product 0"; so on... When I am trying to acces the products: foreach($_SESSION["products"] as $key=>$value) { echo $key."-".$value; } all I get is: 0- 1- 2- ... It doesn't writes the values! I tried to do it multiple ways! What is my mistake? How can I add an array to a session? I googled it and I can't see where is my mistake. Maybe u can help me! Thanks! Quote Link to comment Share on other sites More sharing options...
PHP_PhREEEk Posted December 17, 2007 Share Posted December 17, 2007 $products[] = "product 0"; $products[] = "product 1"; $products[] = "product 2"; $_SESSION['products'] = $products; // foreach loop or whatever you want on $products or $_SESSION['products'] PhREEEk Quote Link to comment Share on other sites More sharing options...
eutu9 Posted December 17, 2007 Author Share Posted December 17, 2007 I tried that! Still not working Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted December 17, 2007 Share Posted December 17, 2007 Try $_SESSION['products'] = serialize($products); Remember to unserialize() it when you need to get it again. Quote Link to comment Share on other sites More sharing options...
PHP_PhREEEk Posted December 17, 2007 Share Posted December 17, 2007 This works... not sure exactly what you want to do, but it displays what it's supposed to.. <?php session_start(); $products[] = "product 0"; $products[] = "product 1"; $products[] = "product 2"; $_SESSION['products'] = $products; foreach ( $_SESSION['products'] as $key => $value ) { echo $key . ' - ' . $value . '<br>'; } ?> PhREEEk Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted December 17, 2007 Share Posted December 17, 2007 My guess is that your code is clearing the values at some point or that register globals are on and $_SESSION['products'] is getting overwritten. Post your actual code. Note: serialize/unserialize is not needed to store arrays in sessions (in fact the session code serializes and unserializes the data when it stores and retrieves if from the session data file.) Quote Link to comment Share on other sites More sharing options...
eutu9 Posted December 17, 2007 Author Share Posted December 17, 2007 I have found the problem! The values were empty because I forgot to fetch the result mysql row ! Sry for bothering you! Next time I will pay more attention! ??? Any code from above works just fine! Thank you for your help and sorry again ! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.