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

时间:2015-11-19 07:08:33

标签: php multidimensional-array

我正在使用phpexcelreader来读取excel文件并获得多维数组作为回报。

现在我想将该记录数组保存到我的数据库中。我正在获取数组,但我不明白如何将每个值都放入数据库。这是我得到的数组

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',
  )

这是我为了获得上面的数组而编写的代码

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>";
        }  */ 
    } 

}

我使用foreach来获取值,但它没有给我所需的结果。

4 个答案:

答案 0 :(得分:0)

要获取数组中的值,请尝试这样,

foreach ($array as $key => $val) {
    foreach ($val as $key1 => $val1) {
         echo $val1[$key1];
    }
}

答案 1 :(得分:0)

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

答案 2 :(得分:0)

<强>代码:

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

?>

<强>输出:

$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

要将记录插入数据库,您可以使用以下代码:

<?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();
?>

如果有帮助,请将此标记为答案,谢谢。 :)

答案 3 :(得分:0)

将您的代码替换为完全动态的代码,无论您的字段是否会在将来附加...

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