简体   繁体   English

将多维数组中的每个值插入到数据库中

[英]Insert each value from multidimensional array into a database

I am using phpexcelreader to read an excel file and getting an multidimensional array in return.我正在使用 phpexcelreader 读取一个 excel 文件并获得一个多维数组作为回报。

Now I want to save that array of records into my database.现在我想将该记录数组保存到我的数据库中。 I am getting the array but I don't understand how I can get each value into the database.我正在获取数组,但我不明白如何将每个值输入到数据库中。 Here is the array which I am getting这是我得到的数组

array (
  1 => 
  array (
    1 => 'Date',
    2 => 'Customer Name',
    3 => 'Address',
    4 => 'Phone  Number',
    5 => 'Email ID',
    6 => 'Amount',
  ),
  2 => 
  array (
    1 => '41577',
    2 => 'Gary E. Ross',
    3 => 'daf, GA 31907',
    4 => '12344',
    5 => 'geross50@gmail.com',
    6 => 'CAD50',
  ),
  3 => 
  array (
    1 => '41577',
    2 => 'JEAN h. LUGAR',
    3 => '123, N.C, 28312',
    4 => '111111',
    5 => 'jhlas@msn.com',
    6 => 'CAD143.28',
  )

This is the code I wrote to get the array above这是我为获取上面的数组而编写的代码

if(move_uploaded_file($_FILES['fileToUpload']['tmp_name'],$target_path))
{
    $read_excel="upload/".$_FILES['fileToUpload']['name'];
    //echo $read_excel;
    $excel = new PhpExcelReader;
    $excel->read($read_excel);
    $data = $excel->sheets[0];
     $excelArray = $excel->sheets[0]['cells'];
    echo '<pre>';
    var_export($excel->sheets[0]['cells']);
    echo '</pre>';

     foreach($excelArray as $key=>$record)
    {
        for($i=1;$i<=count($record);$i++)
        {
            if(isset($record[$i])){
            echo $record[$i]. "</br>";
        }

        }

        /* foreach($record as $innerKey => $innerRecord)
        {   
            echo $innerRecord. "</br>";
        }  */ 
    } 

}

I am using foreach to get the values but it is not giving me the desired result.我正在使用 foreach 来获取值,但它没有给我想要的结果。

To get values in array, try like this,要获取数组中的值,请尝试这样,

foreach ($array as $key => $val) {
    foreach ($val as $key1 => $val1) {
         echo $val1[$key1];
    }
}
foreach ($excelArray as $key=>$val) {
    $insertData='';
    foreach ($val as $key1 => $value) {
        $insertData.= "'".$value."',";
    }
    echo $query = $insertData;
    echo "<br/><br/>";
}

Code:代码:

<?php
$array = array (
          1 => 
          array (
            1 => 'Date',
            2 => 'Customer Name',
            3 => 'Address',
            4 => 'Phone  Number',
            5 => 'Email ID',
            6 => 'Amount',
          ),
          2 => 
          array (
            1 => '41577',
            2 => 'Gary E. Ross',
            3 => 'daf, GA 31907',
            4 => '12344',
            5 => 'geross50@gmail.com',
            6 => 'CAD50',
          ),
          3 => 
          array (
            1 => '41577',
            2 => 'JEAN h. LUGAR',
            3 => '123, N.C, 28312',
            4 => '111111',
            5 => 'jhlas@msn.com',
            6 => 'CAD143.28',
          )
);

echo "<b>\$array values:</b><br/><br/>";

foreach ($array as $key => $val) {
    foreach ($val as $key1 => $val1) {
         //you will get values here.
        echo "<pre>$key1 => $val1</pre>";
    }
    echo "<br/>";
}

?>

Output:输出:

$array values:

1 => Date
2 => Customer Name
3 => Address
4 => Phone  Number
5 => Email ID
6 => Amount

1 => 41577
2 => Gary E. Ross
3 => daf, GA 31907
4 => 12344
5 => geross50@gmail.com
6 => CAD50

1 => 41577
2 => JEAN h. LUGAR
3 => 123, N.C, 28312
4 => 111111
5 => jhlas@msn.com
6 => CAD143.28

To insert the records into database, you'd use following code:要将记录插入数据库,您需要使用以下代码:

<?php
    $a=array (
      1 => 
      array (
        1 => 'Date',
        2 => 'Customer Name',
        3 => 'Address',
        4 => 'Phone  Number',
        5 => 'Email ID',
        6 => 'Amount',
      ),
      2 => 
      array (
        1 => '41577',
        2 => 'Gary E. Ross',
        3 => 'daf, GA 31907',
        4 => '12344',
        5 => 'geross50@gmail.com',
        6 => 'CAD50',
      ),
      3 => 
      array (
        1 => '41577',
        2 => 'JEAN h. LUGAR',
        3 => '123, N.C, 28312',
        4 => '111111',
        5 => 'jhlas@msn.com',
        6 => 'CAD143.28',
      ));

      $columns=$a[1];
      $range=count($a);
      $start=min(array_keys($a))+1; 
      $j=0;

      //echo "<pre>";print_r($a[$start]);exit; 
      for($i=$start; $i<= $range; $i++){
        $data="   ";
        $data.="(";
        foreach($a[$i] as $k=>$v){
            $data.="'".$v."', ";
        }
        $data=substr($data, 0,-2);
        $data.=")";
        $dataArray[$j]=$data;
        $j++;
      }

    $columnsName="( '".implode('\',\'',$columns)."' )";
    $values=implode(',',$dataArray);

    echo $query="INSERT INTO `table_name` ".$columnsName." VALUES ".$values;


    // To insert the records in Database

    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "myDB"; // Database name

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

    $conn->query($query) or die ($conn->error); //This will insert the records into database (table_name)

    $conn->close();
?>

Please mark this as answer if it helped you, Thank you.如果对您有帮助,请将其标记为答案,谢谢。 :) :)

replace your code with this its totally dynamic whether your fields append in future..用这个替换你的代码它是完全动态的,无论你的字段是否在未来追加..

<?php
$a=array (
  1 => 
  array (
    1 => 'Date',
    2 => 'Customer Name',
    3 => 'Address',
    4 => 'Phone  Number',
    5 => 'Email ID',
    6 => 'Amount',
  ),
  2 => 
  array (
    1 => '41577',
    2 => 'Gary E. Ross',
    3 => 'daf, GA 31907',
    4 => '12344',
    5 => 'geross50@gmail.com',
    6 => 'CAD50',
  ),
  3 => 
  array (
    1 => '41577',
    2 => 'JEAN h. LUGAR',
    3 => '123, N.C, 28312',
    4 => '111111',
    5 => 'jhlas@msn.com',
    6 => 'CAD143.28',
  ));
  $columns=$a[1];
  $range=count($a);
  $start=min(array_keys($a))+1; 
  $j=0;
  //echo "<pre>";print_r($a[$start]);exit; 
  for($i=$start; $i<= $range; $i++){
    $data="   ";
    $data.="(";
    foreach($a[$i] as $k=>$v){
        $data.="'".$v."', ";
    }
    $data=substr($data, 0,-2);
    $data.=")";
    $dataArray[$j]=$data;
    $j++;
  }
$columnsName="( '".implode('\',\'',$columns)."' )";
$values=implode(',',$dataArray);
echo $query="Insert into".$columnsName." Values ".$values;  
  ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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