johnmerlino Posted September 3, 2010 Share Posted September 3, 2010 Hey all, I'm well aware of associative arrays (key/value pairs) and how you can index them in an array like this: $character = array (name=>"John", occupation=>"Programmer", age=>22, "Learned language "=>"PHP" ); Then it makes sense to iterate through the key/value pairs of the array using foreach: foreach ( $character as $key=>$val ){ print "$key = $val<br>"; } However, I was watching a video tutorial where he created a $_SESSION array and then created an array associative array: $_SESSION['cart'] = array(); [/code] Then he added a function add_to_cart: function add_to_cart($id){ if(isset($_SESSION['cart'][$id])){ $_SESSION['cart'][$id]++; return true; } else { $_SESSION['cart'][$id] = 1; return true; } return false; } Now that $id variable holds an id converted to integer from the dataabse. So if first item is clicked, id holds a value of integer 1. When I see this: $_SESSION['cart'][$id] I see an array that holds two indexes, each index containing an associative array: [[array() => ''], [1 => '']]. So at index 0 of $_SESSION is [array() => '']. But then he uses the foreach iterator like this: function total_items($cart){ $items = 0; if(is_array($cart)){ foreach($cart as $id => $qty){ $items += $qty; d } } return $items; } Now I'm very confused. As you can see in that foreach method, it says that $id (and its corresponding value) is an associative array of $cart array, not $_SESSION array. I don't see how that happened. I thought $cart and id$ were distinct indexes of $_SESSION. I don't see how $id is a key of the $cart array. Thanks for any explanation to clear my confusion. Quote Link to comment https://forums.phpfreaks.com/topic/212409-question-about-associative-arrays-stored-in-super-global-array-_session/ Share on other sites More sharing options...
PFMaBiSmAd Posted September 3, 2010 Share Posted September 3, 2010 Whatever value (array) was used when total_items(???); was called is what it operates on. That's the point of functions and their parameters. You write a function to perform a specific operation on the data it was passed. As far as the function cares, it does not matter where that actual data came from. Quote Link to comment https://forums.phpfreaks.com/topic/212409-question-about-associative-arrays-stored-in-super-global-array-_session/#findComment-1106695 Share on other sites More sharing options...
btherl Posted September 3, 2010 Share Posted September 3, 2010 If the register_globals option is on (which it is not in modern php distributions), then $cart can be used interchangeably with $_SESSION['cart'] most of the time. The "most of the time" is why register_globals is generally not a good idea. Apart from that, using var_dump() or print_r() can give you a visual idea of the array structure. Then you can see what $_SESSION['cart'][$id] actually looks like. It's an array within another array. The outer array is $_SESSION. The inner array is $_SESSION['cart']. 'cart' is an index in $_SESSION, and $_SESSION['cart'] is its value. $id is in index in $_SESSION['cart'], and $qty is its value. Quote Link to comment https://forums.phpfreaks.com/topic/212409-question-about-associative-arrays-stored-in-super-global-array-_session/#findComment-1106696 Share on other sites More sharing options...
JasonLewis Posted September 3, 2010 Share Posted September 3, 2010 $id = 1; $example = array(); $example['cart'][$id] = 1; What's happening here is we are creating a multi-dimensional array. The parent index (cart) is an associative array and the child indexes (our ids) are numeric arrays. The value for our child indexes are the quantity. A print_r would show something like: Array ( [cart] => Array( [1] => 1 ) ) What the add_cart function is doing is checking if the cart sessions already contains the product id, and if it does simply increment its value (adding another to the cart). If it doesn't then enter it in as 1. The foreach loop which is iterating over the array is this: foreach($_SESSION['cart'] as $id => $qty) Because $_SESSION['cart'] holds an array, and the key of that array is the product id and the value is the quantity, it makes sense to loop over each item this way. The function you showed is receiving the $_SESSION['cart'] as a parameter. So somewhere in the code would be this: $items = total_items($_SESSION['cart']); You need to understand though that an associative array is where you associative a value as the key. $array = array( "name" => "Bill" ); Numeric arrays are where the key is an integer. Well I hope that explains it some what. Good luck. Quote Link to comment https://forums.phpfreaks.com/topic/212409-question-about-associative-arrays-stored-in-super-global-array-_session/#findComment-1106698 Share on other sites More sharing options...
johnmerlino Posted September 3, 2010 Author Share Posted September 3, 2010 Thanks for the responses. Out of curiousity, what makes cart an associative array and not a numeric array if it was defined as an array: $_SESSION['cart'] = array(); And it only contains integer key/value pairs: array([1,1]) Doesn't an associative array have to be an object: array( Title => "rose", Price => 11.25); Maybe I'm confusing this with javascript. Quote Link to comment https://forums.phpfreaks.com/topic/212409-question-about-associative-arrays-stored-in-super-global-array-_session/#findComment-1106828 Share on other sites More sharing options...
johnmerlino Posted September 3, 2010 Author Share Posted September 3, 2010 Or was it the actual assignment operator '=' that converted it to an associative array: $_SESSION['cart'][$id] = 1; If the assignment operator converted it to an associative array, then it makes sense. Otherwise, instantiating a new array alone with array() just creates a regular old numeric array. Quote Link to comment https://forums.phpfreaks.com/topic/212409-question-about-associative-arrays-stored-in-super-global-array-_session/#findComment-1106846 Share on other sites More sharing options...
johnmerlino Posted September 3, 2010 Author Share Posted September 3, 2010 Ok, I see how cart is an associative array here: $_SESSION['cart'] = array(); So super global array $_SESSION: array() associative array cart: array(cart => array()) and then key/value index $id/$qty: array(cart => array([1] => 1)) You would still think intuitively it would be like this though: array(cart => array(1, 1)) But I guess this: $_SESSION['cart'][$id] = 1; makes it like this: array(cart => array([1] => 1)) Quote Link to comment https://forums.phpfreaks.com/topic/212409-question-about-associative-arrays-stored-in-super-global-array-_session/#findComment-1106878 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.