Welcome Guest, Not a member yet? Register   Sign In
Help with array
#1

[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'));
However, then I will store everything on a same 'level' (do not know if this is correct word) in session. For example, it would produce:

Code:
echo '<pre>'; print_r($this->session->userdata); echo '</pre>'

Array
(
    [session_id] => b80a38ba798ba90e735b29c0088ffaae
    [ip_address] => 10.1.0.38
    [user_agent] => Mozilla/5.0 (X11; U; Linux x86_64; en-US) AppleWeb
    [last_activity] => 1288875517
    [choiceX1] => 1
    [choiceX2] => Something
    [choiceX2_price] => 19,00
    [choiceX3] => Fedora
    [choiceX4] => CPanel
    [choiceX4_price] => 20,00
    [choiceX5] => Installatron
    [choiceX5_price] => 7,50
    [choiceX6] => DC USA - Los Angeles
    [choiceX6_price] => Included
    [total] => 46.50
    [servername] => domain.com
)

What I would like to achieve is this:

Code:
Array
(
    [session_id] => b80a38ba798ba90e735b29c0088ffaae
    [ip_address] => 10.1.0.38
    [user_agent] => Mozilla/5.0 (X11; U; Linux x86_64; en-US) AppleWeb
    [last_activity] => 1288875517
    [choiceRealName] => array
                  (
                      [name] = RealName
                      [price] = RealPrice
                   )  
    [choiceOtherName] => array
                     (
                       [name] = OtherName
                       [price] = OtherPrice
                      )
    [total] => 46.50
    [servername] => domain.com
)

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)));
$order = array_merge($order,$choiceX_array);

But, when I want to add another choiceX, it overwrites existing one :-(

Code:
$choiceY_array = array('products' => array('productY' => array('name'=>$name,'price'=$price)));
$order = array_merge($order,$choiceY_array);

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
#2

[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:
&lt;?php
$sess_data = serialize($your_array);
// later on:
$your_array = unserialize($sess_data);
?&gt;

Here more info about serialize:
http://php.net/manual/de/function.serialize.php


You second question - here just a direction Wink
have a look at this synthax for adding values into arrays:

Code:
for ($i=0; $i<10; $i++ ){
$my_array[] = $i;
}
#3

[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 Sad

Thanks!
Smilie
#4

[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(
    'session_id' => 'b80a38ba798ba90e735b29c0088ffaae',
    'ip_address' => '10.1.0.38',
    'user_agent' => 'Mozilla/5.0 (X11; U; Linux x86_64; en-US) AppleWeb',
    'last_activity' => 1288875517,
    'choices' => array(),
    'total' => 0.00,
    'servername' => 'domain.com');
...
$order['choices'][] = array('name' => 'RealName', 'price' => 20.00);
$order['choices'][] = array('name' => 'OtherName', 'price' => 23.50);
...
foreach ($order['choices'] as $choice)
{
...
echo $choice['name'];
...
}




Theme © iAndrew 2016 - Forum software by © MyBB