【问题标题】:array_pop function pops two elements of the arrayarray_pop 函数弹出数组的两个元素
【发布时间】: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


【解决方案1】:

编辑 #3 - 离线讨论后

原来 JS 正在向 PHP 脚本发起第二个请求(在后台),该请求正在减少会话中存储的模板。

具体来说,是 preloader 在图像上循环启动了对 index.php 的额外请求。

img = document.images;
jax = img.length;

for(var i=0; i<jax; i++) {
    console.log(img[i].src);
} 

11:38:39.711 VM322:5 http://plrtesting.herokuapp.com/index.php **这个

11:38:39.711 VM322:5 https://i.imgur.com/gu9bfbD.gif

结束编辑

代码完全按照您的指示去做。

$currentTemplate = array_pop($_SESSION['templates']);

您正在删除,而不是检索最终元素并将其分配给您的变量。每次重新加载页面时,它是数组中的popping 1 个元素。这就是为什么你会看到它随着时间的推移而减少。

您需要改为检索它。如果你想要最后一个元素,那么:

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 = end((array_values($_SESSION['templates'])));

编辑 #1 - 在每次页面加载时随机播放

请注意,有多种方法可以随机化模板。看看这个方式 - Get random item from array.

session_start();
$templates = array("t1.php","t2.php","t3.php"); #list of templates paths

// Commented out the if statement so it shuffles on each page load.
//if (!isset($_SESSION['templates']) || empty($_SESSION['templates'])) {
shuffle($templates); #shuffle them
$_SESSION['templates'] = $templates; #store them in sesssion
//}

$currentTemplate = end((array_values($_SESSION['templates'])));

var_dump($currentTemplate);

EDIT #2 - 不确定它是否清晰,但您的代码正在循环遍历 remaining 元素;不是弹出的元素。

【讨论】:

  • 我试过了,但是模板是静态的,它不会在每次重新加载时弹出,模板也不会随机播放
  • 那是因为当会话为空时,您在第一个页面加载时只有shuffle。在后续页面加载时,会话已设置,因此它不会进入您的 if 语句。
  • 我将在每次页面加载时编辑我的答案以使其成为shuffle
  • 是的,我在我的代码中注意到,当数组为空时。例如。当所有模板都被弹出时,它会弹出 1 个元素。
  • **它不会弹出 2 个元素。它总是只删除/弹出 1 个元素。当你第一次开始时,它从 3 中删除 1,留下 2,然后你循环剩下的 2 个元素。然后你再次重新加载,它从 2 中删除了 1,留下了 1...等等。不确定是否清楚,但您的代码正在循环 **remaining 元素;不是弹出的元素。
猜你喜欢
  • 1970-01-01
  • 2011-06-22
  • 1970-01-01
  • 2014-01-26
  • 1970-01-01
  • 2021-10-16
  • 1970-01-01
  • 1970-01-01
  • 2014-03-16
相关资源
最近更新 更多