我已经搜索了一段时间来完成我的问题,所以我决定在这里提问。
我的问题
我已经用这样一个数据库中的内容填充了2个数组:
$query = "SELECT table_1, table_2 FROM questions";
// Execute query or trow an error
$results = mysqli_query($conn, $query) or die(mysql_error());
$resulttablet1= array();
$resulttablet2= array();
while($row = mysqli_fetch_array($results))
{
// Add the right questions to the array
$resulttablet1[] = $row['table_1'];
$resulttablet2[] = $row['table_2'];
}
所以我现在有两个数组,每个数组都填充了一个表的内容。这一切都很顺利。现在我想把这两个数组放在一个数组中,这样它就像一个大数组。类似这样的东西:
$newarray = array();
$newarray[$resulttablet1, $resulttablet2];
或
$newarray = array($resulttablet1,
$resulttablet2);
然后我想回显$newarray,并显示其他两个数组的所有元素。
我知道我可以分别回应两个数组,但这对于我想要实现的目标来说是不可能的。
提前谢谢。
编辑我意识到我的问题还不够清楚,我会试着更好地解释。
在我的问题之上,我想在单击按钮时逐个显示两个数组的内容。这就是我现在正在做的事情:
// I retrieve a value from a javascript file, where I add 1 to a variable each time a button is clicked then I send this value to the server using jQuery Ajax
$value = (int)$_POST["question_nmbr"];
// I use $value to echo the right element out of the array.
echo "<li>$resulttablet1[$value]</li>";
每次单击按钮时,ajax都会加载php文件,并且该值会增加,从而加载下一个问题。
我想做同样的事情,但现在有一个数组,里面有多个数组
array_merge没有达到我认为的效果,因为print_r($result);给了我数组的所有内容。
我希望我的问题现在更清楚一点。
我找到了答案,多亏有人已经删除了他的答案。
array_merge毕竟做到了,我不知道我第一次做错了什么,但现在我可以很好地回应它了。
以下是它的工作原理:
// I retrieve a value from a javascript file, where I add 1 to a variable each time a button is clicked then I send this value to the server using jQuery Ajax
$value = (int)$_POST["question_nmbr"];
$newarray = array_merge($resulttablet1, $resulttablet2);
echo "<li>$newarray[$value]</li>";
希望有人会发现这很有用,如果不询问更多信息:)