...

View Full Version : Modifying values in a multidimensional array



vsalinas
09-13-2005, 11:57 AM
Hi;

I'm trying to modify some quantities in an array, but obviously I'm doing something wrong.

The original array is as follows;

+---------+---------------------+---------+---------+---------+-----------------
| szruta | fechastk | codprod | cantini | cantfin | desprod
|
+---------+---------------------+---------+---------+---------+-----------------
| 0301500 | 2005-09-13 00:00:00 | 0101 | 20 | 40 | LUCKY 20
|
| 0301500 | 2005-09-13 00:00:00 | 0102 | 15 | 15 | LUCKY 10
|
| 0301500 | 2005-09-13 00:00:00 | 0301 | 10 | 10 | CAPRA
|
+---------+---------------------+---------+---------+---------+-----------------

This array is defined as session_var['stk_movil'].
The following script adds or substract the 'cantfin' field according to the post_vars 'oper' and 'cantoper'.


$codprod = $HTTP_POST_VARS['codprod'];
$cantoper = $HTTP_POST_VARS['cantoper'];
$oper = $HTTP_POST_VARS['oper'];

foreach ($HTTP_SESSION_VARS['stk_movil'] as $r){
if ($r['codprod']==$codprod){

if ($oper == 'RCR')
$r['cantfin']+=$cantoper;
if ($oper == 'DES')
$r['cantfin']-=$cantoper;
if ($oper == 'CAR')
echo 'CARGA';

}
}

But, when I take a look on the modified array, the 'cantfin' remains the same and nothing has changed. I tryied printing the results of the calculation and it was done perfect. The problem is that the array doesn't 'save' the result in the cantfin field.

devinemke
09-13-2005, 02:57 PM
But, when I take a look on the modified array, the 'cantfin' remains the same and nothing has changed. I tryied printing the results of the calculation and it was done perfect. The problem is that the array doesn't 'save' the result in the cantfin field.
because you are never writing the new calculations back to the $HTTP_SESSION_VARS['stk_movil'] array.

i would show you an example but i'm a bit confused about your array structure. what are the keys/values exactly. show the outputs from print_r().

Fou-Lu
09-13-2005, 03:42 PM
Unless your php version is < 4.1, I'd recommend upgrading to superglobals as well, instead of the depreciated $HTTP_*_VARS.
Other than that, array $r is correctly created. Using a var_dump on your $r should create what you need it to, but you'll need to reconstruct your $_SESSION array. This assumes as well that sessions have been created, as your current script does not show this existance.

I'm with Devinemke with your structure as well. I'd assume that your $_SESSION['stk_movil'] is like so (possibly more/less fields):


Array (
[0] => Array (
[szurta] => 0301500
[fechastk] => 2005-09-13 00:00:00
[codprod] => 0101
[cantini] => 20
[cantfin] => 40
[desprod] => LUCKY 20
)
[1] => Array (
[szurta] => 0301500
[fechastk] => 2005-09-13 00:00:00
[codprod] => 0102
[cantini] => 15
[cantfin] => 15
[desprod] => LUCKY 10
)
[2] => Array (
[szurta] => 0301500
[fechastk] => 2005-09-13 00:00:00
[codprod] => 0301
[cantini] => 10
[cantfin] => 10
[desprod] => CAPRA
)
)

If this is the structure you currently have as your $_SESSION, you shouldn't experience any problems (assuming that cantfin is (int) type, or at least a numerical string for comparison).
If you want to update your $_SESSION, like so:


foreach ($_SESSION['stk_movil'] as $r){
if ($r['codprod']==$codprod){

if ($oper == 'RCR')
$r['cantfin']+=$cantoper;
if ($oper == 'DES')
$r['cantfin']-=$cantoper;
if ($oper == 'CAR')
echo 'CARGA';

}
$_SESSION[] = $r;
}

Untested, but I'm thinking that will work. Let us know!

vsalinas
09-13-2005, 08:06 PM
Hi, thanks both for you help, as I'm a begginer in php.
The session_var 'stk_movil' has the following structure (taken from a print_r()):


Array (
[0] => Array (
[0] => 0301500
[szruta] => 0301500
[1] => 2005-09-13 00:00:00
[fechastk] => 2005-09-13 00:00:00
[2] => 0101
[codprod] => 0101
[3] => 20
[cantini] => 20
[4] => 40
[cantfin] => 40
[5] => LUCKY 20
[desprod] => LUCKY 20 )
[1] => Array (
[0] => 0301500
[szruta] => 0301500
[1] => 2005-09-13 00:00:00
[fechastk] => 2005-09-13 00:00:00
[2] => 0102
[codprod] => 0102
[3] => 15
[cantini] => 15
[4] => 15
[cantfin] => 15
[5] => LUCKY 10
[desprod] => LUCKY 10 )
[2] => Array (
[0] => 0301500
[szruta] => 0301500
[1] => 2005-09-13 00:00:00
[fechastk] => 2005-09-13 00:00:00
[2] => 0301
[codprod] => 0301
[3] => 10
[cantini] => 10
[4] => 10
[cantfin] => 10
[5] => CAPRA
[desprod] => CAPRA ) )


After the suggestion of Fou-lu, the array only shows the last row of 'stk_movil':


Array (
[0] => 0301500
[szruta] => 0301500
[1] => 2005-09-13 00:00:00
[fechastk] => 2005-09-13 00:00:00
[2] => 0301
[codprod] => 0301
[3] => 10
[cantini] => 10
[4] => 10
[cantfin] => 10
[5] => CAPRA
[desprod] => CAPRA )

devinemke
09-13-2005, 08:18 PM
foreach ($_SESSION['stk_movil'] as $key => $value)
{
if ($value['codprod'] == $_POST['codprod'])
{
if ($_POST['oper'] == 'RCR') {$_SESSION['stk_movil'][$key]['cantfin'] += $_POST['cantoper'];}
if ($_POST['oper'] == 'DES') {$_SESSION['stk_movil'][$key]['cantfin'] -= $_POST['cantoper'];}
if ($_POST['oper'] == 'CAR') {echo 'CARGA';}
}
}

print_r($_SESSION['stk_movil']);

Fou-Lu
09-14-2005, 04:53 AM
Oh yeah... guess it would help if I actually dumped the variables back into the 'stk_movil' array instead of overwriting them, lol. My bad.

Devinemke's will dump each back into the session superglobal for you.

Now, you need to be using the $_SESSION superglobals for this. As I recall, if your using the old method of registering your session variables, you will need to manually alter your cookies for the changes to take an effect. Don't hold me to that, its been awhile since I used sessions let alone pre-superglobal sessions. Give Devinemke's code a shot, if you have trouble we'll need to see your session setting's methods.

vsalinas
09-14-2005, 01:56 PM
Thank you both!!! everything work perfect now!!!
Also thanks for the suggestion about superglobals, I'll take it into account...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum