我有一个充满随机单词的数组。我的问题是,如何在多个页面中复制array_rand
的值?
因此,刷新后,会根据我的数组生成一个随机值,然后在其他页面上回显相同的值。
我现在的表现显然不起作用。有什么建议吗?
<?php
$elements = array("gold reading glasses","brown sugar","blood","tablet","arrow");
?>
<h1 style="color:red;">
<?php
$generated_element=array_rand($elements,3);
echo $elements[$generated_element[0]]. ", "; echo $elements[$generated_element[1]]. ", "; echo $elements[$generated_element[2]];
?>
</h1>
<h2 style="color:purple;">
<?php
$generated_element=array_rand($elements,3);
echo $elements[$generated_element[0]]. ", "; echo $elements[$generated_element[1]]. ", "; echo $elements[$generated_element[2]];
?>
</h2>
这实际上正是我想要的。在我希望生成数组的第二个实例中,我只是回显变量,并在第二个h2标记下取走了away_rand。
<?php
session_start();
$_SESSION['3e-session']=$elements;
$elements = array("gold reading glasses","brown sugar","blood","tablet","arrow", "key", "curtain rod", "dusty", "sour", "igloo", "musket", "ox", "world war II", "cereal", "green sponge", "casket", "kettle", "handle bar", "cursed", "electronic", "hoist", "stewed", "743", "gold plated", "armored truck", "armor", "grudge");
shuffle($elements);
?>
<h1 style="color:red;"><?php
$generated_element=array_rand($elements,3);
echo $elements[$generated_element[0]]. ", "; echo $elements[$generated_element[1]]. ", "; echo $elements[$generated_element[2]];
?></h1>
<h2 style="color:purple;"><?php
echo $elements[$generated_element[0]]. ", "; echo $elements[$generated_element[1]]. ", "; echo $elements[$generated_element[2]];
?></h2>
答案 0 :(得分:1)
我建议您使用SQL数据库将数组存储为表,而不是尝试创建全局数组。
任何页面都可以查找数据库并访问该表。