Is there a way to do this with a multidimensional array? Example array: Array ( [0] => Array ( [cat_no] => 1033 [desc] => D-Link DP-300U [price_to_cust] => 100.00 [in_stock] => 12 [ordered] => 1 ) [1] => Array ( [cat_no] => 1017 [desc] => PSC QS 6000 Plus [price_to_cust] => 325.00 [in_stock] => 0 [ordered] => 1 ) ) foreach($inv as $item_num){ if($_POST['ordered' . $item_num]){ //echo $_POST['ordered' . $item_num] . '<br>'; $item_num['ordered'] = $_POST['ordered' . $item_num]; }; if('yes' == $_POST['delete' . $item_num]){ //echo 'delete' . $item_num; unset($item_num); };?> <tr> <td><?php echo $item_num['desc'] ?></td> <td><?php echo $item_num['price_to_cust'] ?></td> <td><?php echo $item_num['in_stock'] ?></td> <td><input type="text" size="2" name="<?php echo 'ordered' . $item_num?>" value="<?php echo $item_num['ordered']?>"></td> <td><input type="checkbox" name="<?php echo 'delete' . $item_num?>" value="yes"></td> </tr><?php }; PHP: You can see what i'm trying to do.. but when i echo $item_num it gives me array, which I am assuming is the entire array.
Hi The problem is that you are referencing the multi-dimensional array as if it were single dimensional.. You need to reference the data like this: echo $item_num[ 0 ]['desc']; echo $item_num[ 0 ]['price_to_cust']; echo $item_num[ 0 ]['in_stock']; echo $item_num[ 1 ]['desc']; echo $item_num[ 1 ]['price_to_cust']; echo $item_num[ 1 ]['in_stock']; echo $item_num[ 2 ]['desc']; echo $item_num[ 2 ]['price_to_cust']; echo $item_num[ 2 ]['in_stock']; PHP: Brew
The problem with that is that there could be more items in the cart than that, and I would have to have a variable that has the number in it, correct?
Use sizeof() to determine how big the array is. Try this: for ( $count = 0 ; $count <= sizeof( $item_num ) ; $count++ ) { echo $item_num[ $count ]['desc']; echo $item_num[ $count ]['price_to_cust']; echo $item_num[ $count ]['in_stock']; } PHP: Brew
That is what i had done originally, only with count(). The problem with that was that if I wanted to delete an item, the array would have key's 0,1,3 for example and then it wouldn't show right in the table. Any other ideas?