我有2个php文件。 第一个php:
//store values:
$Hitcount=0;
$result=array();
while statement {
$col = 0;
$result[$Hitcount][$col]=$hit_name;
$result[$Hitcount][++$col]=$hit_link;
$Hitcount++;
}
//check stored values
foreach ($result as $key => $value)
{
foreach ($value as $k => $v)
{
echo '$v'; echo",";
}
}
我从之前的回声中得到以下信息:
gi|1786181|gb|AE000111|ECAE000111,http://www.ncbi.nlm.nih.gov/nuccore/1786181?report=genbank,
gi|1786250|gb|AE000117|ECAE000117,http://www.ncbi.nlm.nih.gov/nuccore/1786250?report=genbank,
gi|1786240|gb|AE000116|ECAE000116,http://www.ncbi.nlm.nih.gov/nuccore/1786240?report=genbank, gi|1786217|gb|AE000114|ECAE000114,http://www.ncbi.nlm.nih.gov/nuccore/1786217?report=genbank,
现在,我想将$ result数组发布到另一个php文件中。 要发布,第一个php包含:
foreach ($result as $key => $value)
{
foreach ($value as $k => $v)
{
echo "<input type='hidden' name='result[$k]' value='$v'/>";
}
}
接收$ result数组,第二个php:
$result = empty($_POST['result']) ? array() : $_POST['result'];
foreach ($result as $key => $value )
{
foreach ($value as $k => $v)
{
echo $v;
}
}
但是,回声给了我:
Invalid argument supplied for foreach().
这意味着第二个foreach()声明。
请帮忙吗?
编辑1: 它现在通过修复第一个PHP作为:
foreach ($result as $key => $value)
{
foreach ($value as $k => $v)
{
echo "<input type='hidden' name='result[$key][$k]' value='$v'/>";
^^^^^^ add second key here
}
}
现在,如何在数据库中保存$ result?我想:
insert row1 in the database and put $result[0][0] in col1 and put $result[0][1] in col2
insert row2 in the database and put $result[1][0] in col1 and put $result[1][1] in col2
insert row3 in the database and put $result[2][0] in col1 and put $result[2][1] in col2
and so on.
现在我尝试:
foreach ($result as $key => $value)
{
foreach ($value as $k => $v)
{
$sql = "INSERT INTO BlastResultFact (Hit) VALUES ('$v')";
$stmt = sqlsrv_query( $conn, $sql);
if( $stmt === false ) {
die( print_r( sqlsrv_errors(), true));
}
}}
it insert row1 in the database and put $result[0][0] in col1 and
insert row2 in the database and put $result[0][1] in col1 and
insert row3 in the database and put $result[1][0] in col1 and so on.
我相信我必须在插入声明中包含col2(HitLink),但是$ v呢?
$sql = "INSERT INTO BlastResultFact (Hit, HitLink) VALUES ('$v')";
答案 0 :(得分:1)
从$_POST['result']
获取的数组是字符串而不是数组。
您需要explode()
这样的字符串:
$result = empty($_POST['result']) ? array() : explode ( '|' ,$_POST['result']);
答案 1 :(得分:1)
问题是您的input
代码是1维的,因此您的内部foreach
将失败,因为$_POST['result']
的每个元素都是字符串,而不是数组
$result = empty($_POST['result']) ? array() : $_POST['result'];
foreach ($result as $key => $value )
{
// $value is a string!
foreach ($value as $k => $v)
{
echo $v;
}
}
要解决此问题,请修改input
代码以生成二维数组:
foreach ($result as $key => $value)
{
foreach ($value as $k => $v)
{
echo "<input type='hidden' name='result[$key][$k]' value='$v'/>";
^^^^^^ add second key here
}
}
现在你应该在$_POST['result']
中获得一个2d数组,并能够使用原始的嵌套循环迭代数组
答案 2 :(得分:0)
您可以使用此方法进行游戏。
foreach($array as $index=>$val)
{
//suppose you have three field
$itemid=$_POST['item'][$index]['value'];
$quantity=$_POST['quantity'][$index]['value'];
$price=$_POST['unitprice'][$index]['value'];
}