array_pop函数弹出数组的两个元素
array_pop function pops two elements of the array
Nothing wrong with the code it works fine but on some templates not.
这是它的工作原理:
我将一组模板存储到会话中,仅当会话为空时才随机播放它们。每次重新加载页面时,我都会弹出会话的一个元素。所以每次页面包含模板时,它都会从数组中弹出
此处的问题,在某些模板上,array_pop
函数会在页面重新加载时弹出数组的 2 个元素(包含的模板 + 另一个)。
我试图删除 "problematic" 模板上的一些代码,但找不到解决方案。
我需要一些帮助来确定这个问题。
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 />';
}
不重新加载
会话['0']的值为't3.php'
会话['1']的值为't2.php'
重新加载 1:
On some templates my code works fine, i repeat. I don't know what is going on :)
编辑 #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 **This one
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);
编辑 #2 - 不确定是否清楚,但您的代码正在遍历 remaining 元素;不是弹出的元素。
Nothing wrong with the code it works fine but on some templates not.
这是它的工作原理:
我将一组模板存储到会话中,仅当会话为空时才随机播放它们。每次重新加载页面时,我都会弹出会话的一个元素。所以每次页面包含模板时,它都会从数组中弹出
此处的问题,在某些模板上,array_pop
函数会在页面重新加载时弹出数组的 2 个元素(包含的模板 + 另一个)。
我试图删除 "problematic" 模板上的一些代码,但找不到解决方案。
我需要一些帮助来确定这个问题。
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 />';
}
不重新加载
会话['0']的值为't3.php'
会话['1']的值为't2.php'
重新加载 1:
On some templates my code works fine, i repeat. I don't know what is going on :)
编辑 #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 **This one
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);
编辑 #2 - 不确定是否清楚,但您的代码正在遍历 remaining 元素;不是弹出的元素。