将邮差多级数组插入数据库

时间:2017-02-17 06:30:25

标签: php mysql arrays postman

我有一个下面提到的数组想要将这个数组插入数据库,数组的键是表列名,每个键都有多个值

Array
(
    [institute_name] => Array
        (
            [0] => ins0
            [1] => ins1
        )

    [name_of_degree] => Array
        (
            [0] => deg0
            [1] => deg1
        )

    [field_of_study] => Array
        (
            [0] => stud0
            [1] => stud1
        )

)

这里有两行插入到数据库中,每个列名都是institute_name,name_of_degree,field_of_study,请建议如何创建一个插入数据库的新数组。

1 个答案:

答案 0 :(得分:0)

我想你想要实现的是这样的事情吗?

<?php

$array["institute_name"][0] = "a";
$array["institute_name"][1] = "b";
$array["institute_name"][2] = "x";

$array["name_of_degree"][0] = "c";
$array["name_of_degree"][1] = "d";
$array["name_of_degree"][2] = "y";

$array["field_of_study"][0] = "e";
$array["field_of_study"][1] = "f";
$array["field_of_study"][2] = "z";


foreach($array["institute_name"] as $key => $value)
{

  $institute_name = $array["institute_name"][$key];
  $name_of_degree = $array["institute_name"][$key];
  $field_of_study = $array["field_of_study"][$key];

$query = "INSERT INTO myTable SET 
institute_name = '$institute_name', name_of_degree = '$name_of_degree', field_of_study = '$field_of_study'; 
";  

echo $query . "<br>";
// $result=$mysqli->query($query); 
// remove above for inserting into the DB   

}
?>