开发者

inserting array elements into database

开发者 https://www.devze.com 2023-02-01 07:57 出处:网络
I want to store Each element of each array into mysql database fields. how can i do that? <?php // primarily a method for storing data

I want to store Each element of each array into mysql database fields. how can i do that?

<?php

// primarily a method for storing data
// arrays are counted from 0

$hosts = array(
array("ronmexico.kainalopallo.com/", "beforename=$F_firstname%20$F_lastname&a开发者_StackOverflow中文版mp;gender=$F_gender",
"Your Ron Mexico Name is ",
"/the ultimate disguise, is ([^<]+)<\/b><\/u>/s"),<u><b>([^<]+)<\/b><\/u>/s"),

array("rumandmonkey.com/widgets/toys/mormon/index.php",
"gender=$F_gender&firstname=$F_firstname&surname=$F_lastname",
"Your Mormon Name is ","/
My <p>My Mormon name is
<b>([^<]+)<\/b>!<br \/>/s")
);

return $hosts;

?>


You could serialize the array and insert the serialized array into the database. Then, we you pull the data out of the database, you would run unserialize and it will turn back into a PHP array.

More info: PHP: serialize


You could try building up the MySQL statement using an array if you already know a Mapping between the Array's Key-Value pairing and the MySQL's table's columns:

$sql = "insert into $table ";
foreach($arr as $key => $value) {
    $sql .= "set $key = $value, ";
}
$sql .= ";";

If you have an array structure like array( array(1,2,3), array(4,5,6), array(7,8,9) );

You know that for($arr as $key => $val) will give you something like [1] => array(1,2,3)

Although I don't think you would be able to save arbritrarily deep arrays using that method. If you have situations where you don't know beforehand how deep the array structure will be, or how it relates to the database, then you should probably save a serialized version like mririgo said.

0

精彩评论

暂无评论...
验证码 换一张
取 消