我们可以使用 PHP SESSION 数组来做到这一点吗?

发布于 2024-10-09 06:00:58 字数 47 浏览 11 评论 0原文

您好,我们可以使用 php 会话创建二维数组吗?如果可能的话如何随机取消设置值。

Hi can we create two dimensional array using php session. If possible how to unset values randomly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天邊彩虹 2024-10-16 06:00:58

不,PHP 不实现多维数组。然而,数组的元素可以是数组本身。并且任何 PHP 数据项都可以存储在会话中(但是,资源在它们初始化所在的线程之外变得毫无意义,并且对象需要从会话中引用类定义)。

例如

<?php

$two_d=array(
    array(1,2,3),
    array(4,5,6),
    array(7,8,9),
    array('#','.','=')
    );
$two_d[3][2]='*'; // was '='

如何随机重置值

这在二维数组中是一个矛盾的说法。但在数组数组的上下文中是完全有效的:

unset($two_d[1]); // removed the whole second row from the above
unset($two_d[0][1]); // $two_d[0] is now array(1,3)

No, PHP does not implement multi-dimensional arrays. However an element of an array can be an array itself. And any PHP data item can be stored in the session (however resources become meaningless outisde the thread they were initialized in, and objects require class definitions to be referenced from the session).

e.g.

<?php

$two_d=array(
    array(1,2,3),
    array(4,5,6),
    array(7,8,9),
    array('#','.','=')
    );
$two_d[3][2]='*'; // was '='

how to unset values randomly

This would be an oxymoron in a 2-dimensional array. But is perfectly valid in the context of an array of arrays:

unset($two_d[1]); // removed the whole second row from the above
unset($two_d[0][1]); // $two_d[0] is now array(1,3)
初与友歌 2024-10-16 06:00:58
$_SESSION['whateverValue'] = Array(
  1 => Array (
    'a','b','c','d'
  ),
  2 => Array (
    'q','w','e','r','t'
  )
);

瞧,会话中的二维数组。

脚本执行时,会话变量没有任何特殊之处。它们唯一的“魔力”是它们在 session_start() 处反序列化并在 session_close() 处序列化

$_SESSION['whateverValue'] = Array(
  1 => Array (
    'a','b','c','d'
  ),
  2 => Array (
    'q','w','e','r','t'
  )
);

Voila, a two-dimensional array, in a session.

The session variables are in no way special while the script is executing. Their only "magic" is that they are unserialized at session_start() and serialized at session_close()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文