Help with array |
[eluser]smilie[/eluser]
Hi, Although I know this is not CI related question (but more PHP in general), I do hope someone will help me out here :-) Problem: I have an order page; On that page, user must order different components of one single product (let's say: RAM, HDD, Traffic etc...). As order proccess is done in 4 steps, whereby on 1st step above input is done - I want to save users choices in session so I can do something with it later on (save in DB). Now, I could store all in session like: Code: $this->session->set_userdata('choiceX',$this->input->post('choiceX')); Code: echo '<pre>'; print_r($this->session->userdata); echo '</pre>' What I would like to achieve is this: Code: Array Reason for this, is that later on, every 'choiceX' will be stored in database as such. Then it is much easier for me do loop the array and do foreach - instead of 'walking' the array manually and looking for keys that 'match'. So - how do I achieve this? What I have tried is: Code: $choiceX_array = array('products' => array('productX' => array('name'=>$name,'price'=$price))); But, when I want to add another choiceX, it overwrites existing one :-( Code: $choiceY_array = array('products' => array('productY' => array('name'=>$name,'price'=$price))); I presume it is really simple to solve this - if you know the trick :-) Could someone please help? An (working) example would be best - but I would be satisfied with a few 'hints / words' so that I can google it up :-) Thanks! Smilie
[eluser]LuckyFella73[/eluser]
For the session question: you can put together you multidimensional array, serialize it and save into 1 session var. When you need the data back just unserialize it and you get back your array. Code: <?php Here more info about serialize: http://php.net/manual/de/function.serialize.php You second question - here just a direction ![]() have a look at this synthax for adding values into arrays: Code: for ($i=0; $i<10; $i++ ){
[eluser]smilie[/eluser]
Hi LuckyFella73, First of all, thanks for your reply. Regarding session and serialization - it definitely sounds like something I could use. I will look into it. As of your second reply / hint. I guess that your code would only work in case array key's are integers. As my key ['products'] is string, it get's overwritten by array_merge() PHP function. Therefore - I do not know how to 'push' another variable to existing ['products']['choiceX'] array ![]() Thanks! Smilie
[eluser]Bart Mebane[/eluser]
If you think about an actual order, the choices themselves are an array, so that's probably the best way to model it: Code: $order = array( |
Welcome Guest, Not a member yet? Register Sign In |