PHP会话数组被覆盖

时间:2012-08-01 09:54:16

标签: php arrays session-variables

我遇到了一个存储在会话变量中的数组的问题。当我尝试对数组中的特定项进行更改时,整个数组将被新值覆盖。

我的代码如下:

<? session_start();

include '../../config/config.php';
include '../functions.php';

if ($user){             
    $type = str_replace("-", "_", ucwords(sanitize($_POST['type'], 'u')));
    $name = str_replace("-", "_", ucwords(sanitize($_POST['name'], 'u')));

    $price = sanitize($_POST['price'], 's');    
    $num = sanitize($_POST['num'], 's');
    preg_match_all('/[-+]?\b[0-9]+(\.[0-9]+)?\b/', $price, $result);                                                            

    if ($num != ""){
        $num = explode("-", $num);

        echo '<pre>';
            print_r($num);
        echo '</pre>';

        echo '<br/>';       

        echo '<pre>';
            print_r($_SESSION['prodcontrolprices']);
        echo '</pre>';              

        switch(count($num)){
            case 2:                                 
                $_SESSION['prodcontrolprices'][$num[0]][$name] = array("price"=>$price);                            
                echo "1here";
                break;
            case 3:                                                                                             
                $_SESSION['prodcontrolprices'][$num[0]][$num[1]]['subs'][$type][$name] = array("price"=>$price);                
                echo "2here";
                break;
            case 4:                 
                $_SESSION['prodcontrolprices'][$num[0]][$num[1]][$num[2]]['subs'][$type][$name] = array("price"=>$price);
                echo "3here";
                break;
            case 5:                 
                $_SESSION['prodcontrolprices'][$num[0]][$num[1]]['subs'][$num[2]][$num[3]]['subs'][$type][$name] = array("price"=>$price);
                echo "4here";
                break;
        }           

        echo '<br/>';

        echo '<pre>';
            print_r($_SESSION['prodcontrolprices']);            
        echo '</pre>';


    }else{
        $_SESSION['prodcontrolprices'][$type][$name] = array("price"=>$price);          
    }
}

?>

以下是echo语句的输出:

Array
(
    [0] => Style
    [1] => One_sided
)

在为其分配值之前的数组内容。

Array
(
    [Style] => Array
        (
            [One_sided] => Array
                (
                    [price] => 130.00
                    [subs] => Array
                        (
                            [Quantity] => Array
                                (
                                    [500] => Array
                                        (
                                            [price] => 10.00
                                        )

                                    [1000] => Array
                                        (
                                            [price] => 20.00
                                        )
                                )
                        )
                )
        )
)

为其分配值后的数组内容。

Array
(
    [Style] => Array
        (
            [One_sided] => Array
                (
                    [price] => 230.00
                )
        )
)

1 个答案:

答案 0 :(得分:4)

你的问题是你用一个新的替换整个子数组(只有一个元素):

$_SESSION['prodcontrolprices'][$type][$name] = array("price"=>$price);

您应该做的只是添加/替换其中的一个值:

$_SESSION['prodcontrolprices'][$type][$name]['price'] = $price;