问题与会话和数组PHP

时间:2018-03-15 22:17:59

标签: php arrays session

我做了一个代码,在会话中添加了商品ID,但是根本不需要,这是代码。

    $my_array=array($_SESSION['buyid']);
    array_push($my_array, $id);
    // put the array in a session variable
    $_SESSION['buyid'] = $my_array;

但以这种方式打印$ _session变量:

$orderamount = $_SESSION['amount'];
            for ($i=1;$i<$orderamount;$i++){
                $order = $_SESSION[$i]['buyid'];
                print "<pre>";
print_r($_SESSION[$i]['buyid']);
print "</pre>";

输出:

            Array
            (
                [0] => Array
                    (
                        [0] => Array
                            (
                                [0] => Array
                                    (
                                        [0] => Array
                                            (
                                                [0] => Array
                                                    (
                                                        [0] => Array
                                                            (
                                                                [0] => 
                                                                [1] => 14
                                                            )

                                                        [1] => 15
                                                    )

                                                [1] => 7
                                            )

                                        [1] => 8
                                    )

                                [1] => 9
                            )

                        [1] => 10
                    )

                [1] => 11
            )

            Array
            (
                [0] => Array
                    (
                        [0] => Array
                            (
                                [0] => Array
                                    (
                                        [0] => Array
                                            (
                                                [0] => Array
                                                    (
                                                        [0] => Array
                                                            (
                                                                [0] => 
                                                                [1] => 14
                                                            )

                                                        [1] => 15
                                                    )

                                                [1] => 7
                                            )

                                        [1] => 8
                                    )

                                [1] => 9
                            )

                        [1] => 10
                    )

                [1] => 11
            )

            Array
            (
                [0] => Array
                    (
                        [0] => Array
                            (
                                [0] => Array
                                    (
                                        [0] => Array
                                            (
                                                [0] => Array
                                                    (
                                                        [0] => Array
                                                            (
                                                                [0] => 
                                                                [1] => 14
                                                            )

                                                        [1] => 15
                                                    )

                                                [1] => 7
                                            )

                                        [1] => 8
                                    )

                                [1] => 9
                            )

                        [1] => 10
                    )

                [1] => 11
            )

            Array
            (
                [0] => Array
                    (
                        [0] => Array
                            (
                                [0] => Array
                                    (
                                        [0] => Array
                                            (
                                                [0] => Array
                                                    (
                                                        [0] => Array
                                                            (
                                                                [0] => 
                                                                [1] => 14
                                                            )

                                                        [1] => 15
                                                    )

                                                [1] => 7
                                            )

                                        [1] => 8
                                    )

                                [1] => 9
                            )

                        [1] => 10
                    )

                [1] => 11
            )

            Array
            (
                [0] => Array
                    (
                        [0] => Array
                            (
                                [0] => Array
                                    (
                                        [0] => Array
                                            (
                                                [0] => Array
                                                    (
                                                        [0] => Array
                                                            (
                                                                [0] => 
                                                                [1] => 14
                                                            )

                                                        [1] => 15
                                                    )

                                                [1] => 7
                                            )

                                        [1] => 8
                                    )

                                [1] => 9
                            )

                        [1] => 10
                    )

                [1] => 11
            )

            Array
            (
                [0] => Array
                    (
                        [0] => Array
                            (
                                [0] => Array
                                    (
                                        [0] => Array
                                            (
                                                [0] => Array
                                                    (
                                                        [0] => Array
                                                            (
                                                                [0] => 
                                                                [1] => 14
                                                            )

                                                        [1] => 15
                                                    )

                                                [1] => 7
                                            )

                                        [1] => 8
                                    )

                                [1] => 9
                            )

                        [1] => 10
                    )

                [1] => 11
            )

            Array
            (
                [0] => Array
                    (
                        [0] => Array
                            (
                                [0] => Array
                                    (
                                        [0] => Array
                                            (
                                                [0] => Array
                                                    (
                                                        [0] => Array
                                                            (
                                                                [0] => 
                                                                [1] => 14
                                                            )

                                                        [1] => 15
                                                    )

                                                [1] => 7
                                            )

                                        [1] => 8
                                    )

                                [1] => 9
                            )

                        [1] => 10
                    )

                [1] => 11
            )

            Array
            (
                [0] => Array
                    (
                        [0] => Array
                            (
                                [0] => Array
                                    (
                                        [0] => Array
                                            (
                                                [0] => Array
                                                    (
                                                        [0] => Array
                                                            (
                                                                [0] => 
                                                                [1] => 14
                                                            )

                                                        [1] => 15
                                                    )

                                                [1] => 7
                                            )

                                        [1] => 8
                                    )

                                [1] => 9
                            )

                        [1] => 10
                    )

                [1] => 11
            )

            Array
            (
                [0] => Array
                    (
                        [0] => Array
                            (
                                [0] => Array
                                    (
                                        [0] => Array
                                            (
                                                [0] => Array
                                                    (
                                                        [0] => Array
                                                            (
                                                                [0] => 
                                                                [1] => 14
                                                            )

                                                        [1] => 15
                                                    )

                                                [1] => 7
                                            )

                                        [1] => 8
                                    )

                                [1] => 9
                            )

                        [1] => 10
                    )

                [1] => 11
            )

            Array
            (
                [0] => Array
                    (
                        [0] => Array
                            (
                                [0] => Array
                                    (
                                        [0] => Array
                                            (
                                                [0] => Array
                                                    (
                                                        [0] => Array
                                                            (
                                                                [0] => 
                                                                [1] => 14
                                                            )

                                                        [1] => 15
                                                    )

                                                [1] => 7
                                            )

                                        [1] => 8
                                    )

                                [1] => 9
                            )

                        [1] => 10
                    )

                [1] => 11
            )

            Array
            (
                [0] => Array
                    (
                        [0] => Array
                            (
                                [0] => Array
                                    (
                                        [0] => Array
                                            (
                                                [0] => Array
                                                    (
                                                        [0] => Array
                                                            (
                                                                [0] => 
                                                                [1] => 14
                                                            )

                                                        [1] => 15
                                                    )

                                                [1] => 7
                                            )

                                        [1] => 8
                                    )

                                [1] => 9
                            )

                        [1] => 10
                    )

                [1] => 11
            )

我认为有一些问题,我需要在正常的数组中知道,以便制作结帐页面,将数据插入到mysql中并进行数学计算和其他检查,但我无法从数组中选择变量。< / p>

1 个答案:

答案 0 :(得分:0)

问题在于$_SESSION[$i],尽管$ _SESSION就像普通数组一样,使用int键迭代它并不是一个好主意。

我假设您要将买家ID添加到会话中,因此代码应如下所示:

// create buyId if it doesn't exist the better name would be "customersIds"
if (!is_array($_SESSION['buyid'])) {
  $_SESSION['buyid'] = [];
}
// assuming that $id is passed somehow to this script when it's not empty add it to array, acts like array_push()
if (!empty($id)) {
    $_SESSION['buyid'][] = $id;
}

它将添加到关键'buyId'下一个ID下的$ _SESSION。如果你想要它们是唯一的,你可以在添加in_array()之前检查,如果id已经存在。

然后,如果您想从会话中获取所有买家ID。

// iterate over array in session and display buyIds
foreach ($_SESSION['buyid'] as $buyId) {
     echo $buyId;
}