我在这里遇到了一个有趣的问题。我有一段代码,我想从不同的数组向数据库发送一些数据。问题是我有很多代码重复,所以我试图用它来实现一个函数
$i = "0";
$education = $user['education'][$i]['school']['name'];
while ($education != null){
mysql_query("INSERT INTO educations VALUES(
{$user['id']},
'{$user['education'][$i]['school']['name']}'
)");
}
$i++;
$education = $user['education'][$i]['school']['name'];
}
我希望它能这样工作:
$i = "0";
$item = $user['education'][$i]['school']['name'];
$table = 'educations'
$current = $item;
while ($current != 'EOL'){
if ($current != null){
mysql_query("INSERT INTO {$table} VALUES(
{$user['id']},
'{$item}'
)");
}
$i++;
$current = $item;
}
我要做的是将变量$user['education'][$i]['school']['name']
保存在$item
中,这样,每当我将$item
存储在另一个变量中时,它就会再次插入$i
的值。我不知道这是否可能,但这是一件有趣的事情。
对于变量名,您可以使用类似${'myvariable_'.$i}的东西,它将输出$myvariable_0,_1。。。对于数组,我认为它不起作用(你能试试吗?),否则你应该使用这样的函数:
function getNthSchoolName($n) {
global $user;
return $user[''][$n]['schoool']['name'];
}