未设置多维数组 [英] unset multidimensional array

查看:122
本文介绍了未设置多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有与此一时间地狱。我有我在一个会话存储我多维数组。

I am having a hell of a time with this. I have a multidimensional array that I am storing in a session.

$d1 = array(1,2,3,4);
$d2 = array(1,2,3,4,5,6);
$d3 = array(1,2,3,4,5,6,7,8);
$d4 = array(1,2,3,4,5);
$_SESSION['array1'] = array($d1,$d2,$d3,$d4);

我想要做的是去除会话ARRAY1的$ D2阵列

what I want to do is remove the $d2 array from the session array1

但是当我做这样的事情。

however when I do something like this

unset($_SESSION['array1'][1]); 

你会认为,$ _SESSION ['数组1']然后会=阵列($ D1,D3 $,$ D4);

you would think that $_SESSION['array1'] would then = array($d1,$d3,$d4);

但是,做什么是真正取消了整个会话变量。

however what that does is actually unset the whole session variable.

然后,如果我尝试像

foreach ($_SESSION['array1'] as $k => $v) {
echo "The Key is $k: The Value is $v";
}

不过,让我一个错误

however that gives me an error

有关的foreach提供了无效的参数()

Invalid argument supplied for foreach()

这是我可以得出的唯一结论是,会话变量被完全取消设置,而不是只是特定的键被从阵列中删除。

The only conclusion that I can come to is that the session variable is being completely unset, not that just the specific key is being removed from the array.

在那里,我可以不设置包含一个数组,是一个会话变量的一部分内的特定值什么办法?

is there any way that i can unset a specific value contained within an array that is part of a session variable?

推荐答案

code你present按预期工作:

Code you present works as expected:

header("Content-Type: text/plain");
session_start();
$d1 = array(1,2,3,4);
$d2 = array(1,2,3,4,5,6);
$d3 = array(1,2,3,4,5,6,7,8);
$d4 = array(1,2,3,4,5);
$_SESSION['array1'] = array($d1,$d2,$d3,$d4);
unset($_SESSION['array1'][1]); 
print_R($_SESSION);

打印:

Array
(
    [array1] => Array
        (
            [0] => Array
                (
                    ...
                )

            [2] => Array
                (
                    ...
                )

            [3] => Array
                (
                    ...
                )

        )

)

因此​​,一些调试的想法:

So some debugging ideas:


  1. 请不要使用 @session_start

  2. 设置的error_reporting(E_ALL)

  3. 在php.ini中配置错误报告

  4. 检查cookie来看看PHPSESSID cookie是否在所有发送。

这篇关于未设置多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆