简体   繁体   中英

insert postman multilevel array into database

I have an array mentioned below want to insert this array into database, the keys of the array are table column name and each key have multiple values

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

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

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

)

Here two rows are inserted into the database for each column names are institute_name, name_of_degree, field_of_study, please suggest how to create a new array that inserts into the database.

I guess it is something like this you are trying to achieve?

<?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   

}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM