简体   繁体   中英

Import data from multi-dimensional array

I have retrieved data from database. Structure looks like this.

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

I would like to import this data into my database. Which has the same structure. ID, name , surname , age. I understand there must be some kind of loop, but I cant really manage to get there.

Using foreach loop you can...

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

Output,

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

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