Using variables as array keys

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Tarantulus
    New Member
    • May 2007
    • 114

    Using variables as array keys

    Hi Ladies/Gents/Other

    I'm having issues with nested arrays, I need to set an array key by variable but whenever I do I get an error "invalid offset in XXXXX"

    here is the code causing the problem

    Code:
    <?php
    
    class cart{
    	
    	function add($prod_id,$qty,$price) {
    		$prod_id=array();
    		if (!isset ($_SESSION['cart'])){
    			session_start();
    			$_SESSION['cart']=array($prod_id);
    		}
    		if (isset ($_SESSION['cart'][$prod_id])){
    			$_SESSION['cart'][$prod_id]['qty'] =+ 1;
    		} 
    		else{
    			$_SESSION['cart'][$prod_id]=$prod_id;
    			$_SESSION['cart'][$prod_id]['qty']=1;
    			$_SESSION['cart'][$prod_id]['price']=$price;
    		}
    		print_r($_SESSION['cart'][$prod_id$]);
    	}
    };
    
    $cart = new cart;
    
    $cart -> add('jfkd',2,22.50);
    can someone please point out what I'm doing wrong?

    Thank you in advance
  • realin
    Contributor
    • Feb 2007
    • 254

    #2
    Hi check out, this piece of code works for me..
    you can specify a key to be an array.. A key can be a string or numeric value..
    Though u can assign an associate array to a session variable.. like ::
    [PHP]$_SESSION["arr"]=$assoc_array;
    [/PHP]

    Here is the code ::

    [PHP]<?php session_start() ;
    class cart{

    function add($prod_id,$q ty,$price) {

    //$prod_id=array( );
    if (!isset ($_SESSION['cart'])){
    session_start() ;
    $_SESSION['cart']=array($prod_id );
    }
    if (isset ($_SESSION['cart'][$prod_id])){
    $_SESSION['cart'][$prod_id]['qty'] =+ 1;
    }
    else{
    $_SESSION['cart'][$prod_id]=$prod_id;
    $_SESSION['cart'][$prod_id]['qty']=1;
    $_SESSION['cart'][$prod_id]['price']=$price;
    }
    print_r($_SESSI ON['cart'][$prod_id]);
    }
    };
    $cart = new cart;
    $cart -> add('jfkd',2,22 .50);
    ?>[/PHP]

    hope this helps,
    cheers !!
    Realin !!

    Comment

    • hsriat
      Recognized Expert Top Contributor
      • Jan 2008
      • 1653

      #3
      Originally posted by Tarantulus
      Hi Ladies/Gents/Other
      LOL..

      Originally posted by Tarantulus
      I'm having issues with nested arrays, I need to set an array key by variable but whenever I do I get an error "invalid offset in XXXXX"
      Code:
      class cart
      {
      	
      	function add($prod_id,$qty,$price) 
      	{
      	
      		//$prod_id=array(); //reseting the argument?..!! very bad!!
      		
      		if (!isset ($_SESSION['cart']))
      		{
      
      			session_start();
      			//just start the session.. nothing else
      			//$_SESSION['cart']=array($prod_id);
      			
      		}
      		elseif (!isset($_SESSION['cart'][$prod_id]))
      		{
      			$_SESSION['cart'][$prod_id] = array("qty"=>1,"price"=>$price);
      		}
      		else
      		{
      			$_SESSION['cart'][$prod_id]['qty'] += 1;
      			$_SESSION['cart'][$prod_id]['price'] = $price;
      		}
      		//nothing else
      		//else{
      		//	$_SESSION['cart'][$prod_id]=$prod_id;
      		//	$_SESSION['cart'][$prod_id]['qty']=1;
      		//}
      		print_r($_SESSION['cart'][$prod_id]);
      	}
      };
      
      $cart = new cart;
      
      $cart -> add('jfkd',2,22.50);

      Comment

      • Tarantulus
        New Member
        • May 2007
        • 114

        #4
        Thanks a lot guys, works a charm now!

        Comment

        • realin
          Contributor
          • Feb 2007
          • 254

          #5
          Originally posted by Tarantulus
          Thanks a lot guys, works a charm now!
          anytime mayne :)
          hope to see you around

          Comment

          Working...