将多维数组插入mysql

时间:2011-07-21 21:42:39

标签: php mysql multidimensional-array

我想将多维数组中的数据插入到mysql中,但在循环时循环超过必要的数量。我的意思是我希望它输入6条记录,但我有很多记录。我知道我的mysql查询位置有问题,但尝试了很多技术,但都无效。这是我的代码 请帮帮我

<?php
$primary = array(
    "array 1"=> array("clas" => "val 1","pupil" => "val 2","subject" => "val 3"),
    "array 2 "=> array("clas" => "val 1","pupil" => "val 2","subject" => "val 3"),
    "array  3" =>  array("clas" => "val 1","pupil" => "val 2","subject" => "val 3"),
    "array  4" => array("clas" => "val 1","pupil" => "val 2","subject" => "val 3"),
    "array  5" =>  array("clas" => "val 1","pupil" => "val 2","subject" => "val 3"),
    "array  6" =>  array("clas" => "val 1","pupil" => "val 2","subject" => "val 3"),
);
foreach($primary as $t => $value){
    $class =$t;
    $clas = $primary[$t]["clas"];
    $pupil =  $primary[$t]["pupil"];
    $sub =$primary[$t]["subject"];
    mysql_query("insert into tablename( f1, f2, f3) values('$clas','$pupil','$sub')");
}

2 个答案:

答案 0 :(得分:2)

您可以尝试序列化它们并将它们存储为blob。序列化会将它们变成一个巨大的字节数组,您可以检索和反序列化。

答案 1 :(得分:0)

如果您不需要搜索数据,那么序列化数组实际上并不是最糟糕的想法。另一种选择是为数据选择另一种数据结构,例如二叉树和对象。您可以序列化和反序列化的对象,因为它是一个二叉树,您可以在内存中快速搜索它。这是我实际上想到自己的事情。