【发布时间】:2018-05-08 15:43:56
【问题描述】:
代码没有问题,它可以正常工作,但在某些模板上却不行。
这是它的工作原理:
我将一组模板存储到会话中,并且仅当会话为空时才对它们进行洗牌。每次重新加载页面时,我都会弹出会话的一个元素。所以每次页面中包含一个模板时,都会从数组中弹出
这里的问题是,在某些模板上,array_pop 函数会在页面重新加载时弹出数组的 2 个元素(包含的模板 + 另一个)。
我试图删除“有问题”模板上的一些代码,但我找不到解决方案。
我需要一些帮助来识别这个问题。
session_start();
$templates = array("t1.php","t2.php","t3.php"); #list of templates paths
if (!isset($_SESSION['templates']) || empty($_SESSION['templates'])) {
shuffle($templates); #shuffle them
$_SESSION['templates'] = $templates; #store them in sesssion
}
$currentTemplate = array_pop($_SESSION['templates']); #pops one on each page reload
include $currentTemplate; #includes the next template of the array
#on each page reload an element will be popped out and the next one will be included, the issue, is that sometimes two elements-templates are popped out of the array.
我检测到它通过以下代码弹出两个元素:
foreach($_SESSION['templates'] as $key=>$value)
{
echo 'The value of session['."'".$key."'".'] is '."'".$value."'".' <br />';
}
不重新加载
session['0']的值为't3.php'
session['1']的值为't2.php'
重新加载 1:
在某些模板上,我的代码运行良好,我重复一遍。不知道怎么回事:)
【问题讨论】:
-
你知道
array_pop()改变了原来的数组,对吧?它返回值并将其从数组中删除。 -
你需要展示什么不工作以及你认为它是如何弹出 2 和/或一些调试。
-
@AbraCadaver 我更新了我的代码,让你更好地理解。请注意,代码很好,这只发生在某些模板上,我需要帮助如何识别问题。
标签: php arrays debugging shuffle