我想从多维数组插入数据到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')");
}
您可以尝试将它们序列化并将它们存储为blob。序列化将它们变成一个巨大的字节数组,您可以检索和反序列化。
如果您不需要搜索数据,那么序列化数组实际上并不是最坏的主意。另一种选择是为数据选择另一种数据结构,例如二叉树和对象。你可以序列化和反序列化这个对象,因为它是一个二叉树,所以你可以在内存中快速搜索它。这是我对自己的看法。