从数组中获取数据并将其保存在数据库中

时间:2016-07-13 05:55:00

标签: mysql arrays multidimensional-array foreach

我有这个代码

echo "<pre>";print_r($products[$i]);

通过它获得以下数据

echo "<pre>";
print_r($data);
echo "</pre>";

我需要numCols所以我使用了以下代码

Spreadsheet_Excel_Reader Object
    (
        [sheets] => Array
            (
                [0] => Array
                    (
                        [maxrow] => 0
                        [maxcol] => 0
                        [numRows] => 2
                        [numCols] => 5
                        [cells] => Array
                            (
                                [1] => Array
                                    (
                                        [1] => sno
                                        [2] => candidta name
                                        [3] => email
                                        [4] => ctc
                                        [5] => location
                                    )
                            )
                    )
            )
    )

得到结果echo $data->sheets[0]['numCols'];

现在我想获取以下值

5

并将它们存储在表格中

[1] => sno                                     
[2] => candidta name
[3] => email
[4] => ctc
[5] => location

任何人都可以告诉我们该怎么做

2 个答案:

答案 0 :(得分:0)

$newarray = $data->sheets[0]['cells'][1];
for ( $i=1; $i < count($newarray); $i++)
    { 
        echo $newarray[$i];

        $sql= "INSERT INTO `tablename` (value) VALUES ('$newarray[$i]')";
        if(!mysqli_query($con,$sql))
            {
                echo "Error: " . $sql. "<br>" . mysqli_error($con);
            }  
    } 

答案 1 :(得分:-1)

$Data = array();
foreach($data->sheets[0]['cells']['1'] as $key=>$value):
    $Data[] = "('".$key."','".$value."')";
endforeach;
mysql_query("INSERT INTO tbl_name(`id`,`value`) VALUES".implode(",",$Data)) or die (mysql_error());