这是一个输出示例(没有项目符-6ren">
gpt4 book ai didi

php - 使用 PHP 将数组值分配给增量变量以插入到 mysql 表中

转载 作者:行者123 更新时间:2023-12-01 00:41:16 25 4
gpt4 key购买 nike

我从以前的表单中得到以下结果页面:

<?php
//Get the form results (which has been converted to an associative array) from the $_POST super global
$musicgenres = $_POST['music'];

//Sort the values by rank and keep the key associations.
asort($musicgenres, SORT_NUMERIC );

//Loop over the array in rank order to print out the values.
foreach($musicgenres as $music => $rank)
{
echo "$music is your $rank choice";
echo "<br>";
}
?>

这是一个输出示例(没有项目符号):

  • 说唱是你的第一选择
  • 摇滚是你的第二选择
  • 爵士乐是您的第三选择
  • 等....

我认为将其放入表格中既不容易又干净。如何将这些值分配给增量变量?

例如

  • $musicrank1 = 说唱
  • $musicrank2 = 摇滚
  • $musicrank3 = 爵士乐
  • 等....

最佳答案

试试这个

$i = 0;
//Loop over the array in rank order to print out the values.
foreach($musicgenres as $music => $rank)
{
${ "musicrank". ($i) } = $music;
echo "$music is your $rank choice";
echo "<br>";
$i++;
}

在 foreach 之后,您将拥有自己的变量。

关于php - 使用 PHP 将数组值分配给增量变量以插入到 mysql 表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31938310/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com