您可以创建一个如下所示的数组:
$arr = array(
'parent1' => array('child1'),
'parent2' => array('child1', 'child2'),
'parent3' => array('child1', 'child2', 'child3')
);
// Debugging...
var_dump(array_keys($arr)); // These are the parents
var_dump(array_values($arr)); // And these are their children
然后,插入:
var_dump('INSERT INTO `parents` (`name`) VALUES ("'.implode('"), ("', array_keys($arr)).'");'); // These are the parents
foreach ($arr as $parent => $children)
var_dump('INSERT INTO `children` (`parent_name`, `name`) VALUES ("'.$parent.'", "'.implode('"), ("'.$parent.'", "', array_keys($children)).'");'); // These are the children
更改var_dump()
为用于运行查询的相应方法。