Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/251.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP为增量变量分配数组值以插入mysql表_Php_Mysql_Arrays_Loops - Fatal编程技术网

使用PHP为增量变量分配数组值以插入mysql表

使用PHP为增量变量分配数组值以插入mysql表,php,mysql,arrays,loops,Php,Mysql,Arrays,Loops,我有上一张表格的以下结果页: <?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

我有上一张表格的以下结果页:

<?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>";
 }
?>

下面是一个输出示例(不带项目符号):

  • 说唱是你的首选
  • 摇滚是你的两个选择
  • 爵士乐是你的三个选择
  • 等等
我想把它放在桌子上既不容易也不干净。如何将这些值分配给增量变量

e、 g

  • $1=rap
  • $2=摇滚乐
  • $MusicCrank3=爵士乐
  • 等等
试试这个

$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++;
 }
$i=0;
//按秩在数组上循环以打印值。
foreach($musicgenres as$music=>$rank)
{
${“MusicCrank.”($i)}=$music;
echo“$音乐是您的$rank选择”;
回声“
”; $i++; }
foreach之后,您将拥有您的变量。

试试这个

$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++;
 }
$i=0;
//按秩在数组上循环以打印值。
foreach($musicgenres as$music=>$rank)
{
${“MusicCrank.”($i)}=$music;
echo“$音乐是您的$rank选择”;
回声“
”; $i++; }

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

使用表比创建任意变量容易得多。对于该表,您只需说出
$musicrank[1]
而不是
$musicrank 1
,如果需要,您可以在该表上进行迭代


根据经验,如果手动分配表中的每个值,则应使用变量。但是您使用的是循环,因此对于表来说这是一个非常明显的情况。

使用表比创建任意变量容易得多。对于该表,您只需说出
$musicrank[1]
而不是
$musicrank 1
,如果需要,您可以在该表上进行迭代


根据经验,如果手动分配表中的每个值,则应使用变量。但是您使用的是一个循环,因此对于表来说这是一个非常明显的例子。

使用以下脚本:for add array with comma

        `$no = 0;
        foreach($sql2 as $row2)
        {
            $row["store_name"] .= ($no != 0) ? ", " : "";
            $row["store_name"] .= $row2["store_name"];

            $no++;
        }

使用以下脚本:用于添加带有逗号的数组

        `$no = 0;
        foreach($sql2 as $row2)
        {
            $row["store_name"] .= ($no != 0) ? ", " : "";
            $row["store_name"] .= $row2["store_name"];

            $no++;
        }

你到底想在这里实现什么?动态创建数量不确定的变量看起来像是一个黑客。制作一个music_ranks表并插入rank和music。然后做任何你想做的事情。如果是动态的,那么使用query更新music_rank表。您在这里到底想实现什么?动态创建数量不确定的变量看起来像是一个黑客。制作一个music_ranks表并插入rank和music。然后做任何你想做的事情。如果是动态的,则使用query更新music_rank表。