Php 从多维数组中删除最后一项并将剩余项插入MySql

Php 从多维数组中删除最后一项并将剩余项插入MySql,php,mysql,arrays,multidimensional-array,Php,Mysql,Arrays,Multidimensional Array,我有一个这样的多维数组。 [rows] => Array ( [0] => Array ( [0] => fname1 [1] => flabel1 [2] => numeric [3] => yes,no [4] => h1

我有一个这样的多维数组。

 [rows] => Array
    (
        [0] => Array
            (
                [0] => fname1
                [1] => flabel1 
                [2] => numeric 
                [3] => yes,no
                [4] => h1 
                [5] => Y 
                [6] => a1
                [7] => <input id="1" value="remove" type="button">
            )

        [1] => Array
            (
                [0] => fname2
                [1] => flabel2 
                [2] => text 
                [3] => yes,no
                [4] => h2 
                [5] => Y 
                [6] => a2 
                [7] => <input id="2" value="remove" type="button">
            )

        [2] => Array
            (
                [0] => fname3
                [1] => flabel3 
                [2] => text 
                [3] => yes,no
                [4] => h3 
                [5] => N 
                [6] => a3  
                [7] => <input id="3" value="remove" type="button">
            )

    )
我试过这个

$rows = $_REQUEST['rows'];
foreach($rows AS $row)
{
    print_r($row);
}
这是给我的数组值。但是如何插入MySql。

尝试使用以下方法:

unset ($array_name[count($array_name)-1]);
$rows = $_REQUEST['rows'];
    foreach($rows AS $row)
    {
      array_pop($row);//remove last element
        print_r($row);

    //since you have fixed index then your sql will become.
    $sql = "INSERT INTO section_details (fieldname, fieldlabel, fieldtype, fieldoptions, hint, required, actions) VALUES ('$row[0]','$row[1]','$row[2]','$row[3]','$row[4]','$row[5]','$row[6]') ";
        }
尝试使用以下方法:

$rows = $_REQUEST['rows'];
    foreach($rows AS $row)
    {
      array_pop($row);//remove last element
        print_r($row);

    //since you have fixed index then your sql will become.
    $sql = "INSERT INTO section_details (fieldname, fieldlabel, fieldtype, fieldoptions, hint, required, actions) VALUES ('$row[0]','$row[1]','$row[2]','$row[3]','$row[4]','$row[5]','$row[6]') ";
        }

您可以在foreach中删除数组中的最后一个元素&将数组内爆为字符串并使用sql

foreach ($rows as $row)
{
   unset($row[count($row)-1]);
   $mysqlStr = implode(',',$row);
   $sql = "INSERT INTO section_details (fieldname, fieldlabel, fieldtype, fieldoptions, hint, required, actions) VALUES ({$mysqlStr}) ";

 }

您可以在foreach中删除数组中的最后一个元素&将数组内爆为字符串并使用sql

foreach ($rows as $row)
{
   unset($row[count($row)-1]);
   $mysqlStr = implode(',',$row);
   $sql = "INSERT INTO section_details (fieldname, fieldlabel, fieldtype, fieldoptions, hint, required, actions) VALUES ({$mysqlStr}) ";

 }
这将在单个查询中存储数据。 您应该使用它来验证值

这将在单个查询中存储数据。
您应该使用来验证值。

您的插入查询中没有值。值是多维数组中的数组值,我无法将其放入。您的插入查询中没有值。值是多维数组中的数组值,我无法将其放入。
$sql = "INSERT INTO section_details (fieldname, fieldlabel, fieldtype, fieldoptions, hint, required, actions) VALUES ";
$values = array();
foreach($rows as $row){
   unset($row[count($row)-1]);
   $values[] = implode('", "', $row);
}

$sql .= '( "'.implode('", "', $values).'")';