[英]PHP Echo just one the item from the array, if reload do not echo
I have an array of values with PHP, Is it possible that when the user first reloads the page, the value should be echoed, but If he reloads the same page again, It should not output two of the same values.我有一个带有 PHP 的值数组,是否有可能当用户第一次重新加载页面时,应该回显该值,但是如果他再次重新加载同一页面,则不应 output 两个相同的值。
foreach($_SESSION['arrayFolders'] as $value){
if (in_array($_SESSION['arrayFolders'], $value)) {
echo 'Do not reload the page, we have already greeted you :)';
} else {
echo 'Welcome!';
}
What I am trying to do with the IF condition is, if the value is already on the session array do not echo it.我试图对 IF 条件做的是,如果该值已经在 session 数组上,则不要回显它。
NOTE: When the user first enters on the page, he generates an array called $_SESSION['arrayFolders']
.注意:当用户第一次进入页面时,他会生成一个名为
$_SESSION['arrayFolders']
的数组。
Any questions feel free to ask.有任何问题请随时询问我(们)。
EDIT 1:编辑1:
Reverse the in_array
method.反转
in_array
方法。
...
$_SESSION['arrayFolders'][] = '<a>folder/folder2</a>';
...
foreach($_SESSION['arrayFolders'] as $value){
if (in_array($value, $_SESSION['arrayFolders'])) {
echo 'Welcome';
//Here I should delete the value from the string so that It does not loop again.
}
Make it an associative array, where the messages are the keys, and the values are boolean flags indicating whether the message has been shown.使其成为一个关联数组,其中消息是键,值是 boolean 标志,指示消息是否已显示。 When a message is added to the array, the value is
false
.将消息添加到数组时,值为
false
。 Then when the page is loaded, you only show these values, and set their values to true
to prevent them being shown again.然后当页面加载时,您只显示这些值,并将它们的值设置为
true
以防止它们再次显示。
foreach($_SESSION['arrayFolders'] as $message => &$flag){
if (!$flag) {
echo $message;
$flag = true;
}
}
unset ($flag); // remove the reference to the array element
Using the reference variable &$flag
makes the assignment update the $_SESSION
array.使用引用变量
&$flag
使赋值更新$_SESSION
数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.