Make an array key a variable

Discussion in 'PHP' started by Greenmethod, Oct 24, 2007.

  1. #1
    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.
     
    Greenmethod, Oct 24, 2007 IP
  2. Brewster

    Brewster Active Member

    Messages:
    489
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    60
    #2
    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
     
    Brewster, Oct 24, 2007 IP
  3. Greenmethod

    Greenmethod Peon

    Messages:
    112
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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?
     
    Greenmethod, Oct 24, 2007 IP
  4. Brewster

    Brewster Active Member

    Messages:
    489
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    60
    #4
    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
     
    Brewster, Oct 24, 2007 IP
  5. Greenmethod

    Greenmethod Peon

    Messages:
    112
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    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?
     
    Greenmethod, Oct 24, 2007 IP
  6. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #6
    
    foreach (array_keys($item_num) AS $key)
    {
        echo $item_num[$key]['desc'];
    }
    
    PHP:
    ???
     
    nico_swd, Oct 24, 2007 IP