somedude084 Posted January 7, 2010 Share Posted January 7, 2010 Hello. I have a form which posts an array to this script. However, I can't seem to access the values in the array; I keep getting an undefined index. Something is not right, but the code is so simple! What am I missing? print_r($_POST['Item'][0]); // Output is: Array ( ['Quantity'] => 1 ['Item_id'] => 5 ) echo $_POST['Item'][0]['Quantity']; // Output is: Notice: Undefined index: Quantity in /Volumes/Macintosh HD/Sites/.../hal/item/reconcile.php on line 30 Link to comment https://forums.phpfreaks.com/topic/187634-baffled-by-undefined-index-in-simple-array-please-help/ Share on other sites More sharing options...
ignace Posted January 7, 2010 Share Posted January 7, 2010 It means Quantity does not exist. And this code is not possible: echo $_POST['Item'][0]['Quantity']; It's: echo $_POST['Item'][0]; //or echo $_POST['Item']['Quantity']; Link to comment https://forums.phpfreaks.com/topic/187634-baffled-by-undefined-index-in-simple-array-please-help/#findComment-990621 Share on other sites More sharing options...
somedude084 Posted January 7, 2010 Author Share Posted January 7, 2010 Thanks for the response, but not sure that I follow. Perhaps my syntax is wrong, but there has to be some way to access values from an array containing an array. As shown by the code example, this is a multi-dimensional array, so $_POST['Item'][0] (the "top-level" array's item) itself contains an array with key-value pairs (as shown by the first line of output): what I am trying to do is access the value related to the 'Quantity' key in that "sub-array." You seem to imply that multi-dimensional arrays are not possible? This is not per my understanding. Can anyone help? Much appreciated! Link to comment https://forums.phpfreaks.com/topic/187634-baffled-by-undefined-index-in-simple-array-please-help/#findComment-990659 Share on other sites More sharing options...
somedude084 Posted January 10, 2010 Author Share Posted January 10, 2010 Figured this out on my own: Because the array is multi-dimensional, you need two nested foreach loops to access the inner array: foreach($_POST['Item'] as $post_key) { $query_string .= " ("; foreach($post_key as $post_sub) { $query_string.= "'".$post_sub."', "; } } Link to comment https://forums.phpfreaks.com/topic/187634-baffled-by-undefined-index-in-simple-array-please-help/#findComment-992204 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.