2017-06-22 83 views
0

我已經從數據庫中檢索數據。結構看起來像這樣。從多維數組導入數據

$array = array (array ('name'=>'oscar', 'surname'=>'trump', 'age'=>'40'),array ('name'=>'ben', 'surname'=>'simmons', 'age'=>'22'), array ('name'=>'daniel', 'surname'=>'green', 'age'=>'12')); 

我想將這些數據導入到我的數據庫中。它具有相同的結構。身份證,姓名,年齡。我知道必須有某種循環,但我無法真正設法到達那裏。

回答

1

使用foreach循環,你可以...

<?php 
    $array = array (array ('name'=>'oscar', 'surname'=>'trump', 'age'=>'40'),array ('name'=>'ben', 'surname'=>'simmons', 'age'=>'22'), array ('name'=>'daniel', 'surname'=>'green', 'age'=>'12')); 
    foreach($array as $field=>$values){ 
     echo "insert into `table_name` set `name`='".$values['name']."', `surname`='".$values['surname']."', `age`='".$values['age']."';"; 
     echo "<br />"; 
    } 
?> 

輸出,

insert into `table_name` set `name`='oscar', `surname`='trump', `age`='40'; 
insert into `table_name` set `name`='ben', `surname`='simmons', `age`='22'; 
insert into `table_name` set `name`='daniel', `surname`='green', `age`='12';