Jump to content

Baffled by Undefined Index in Simple Array: Please Help!


somedude084

Recommended Posts

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

 

:wtf:

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!

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."', ";

}

}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.