[英]How to echo multidimentional array posted from php
我有2個php文件。 第1個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')";
您從$_POST['result']
獲取的數組是字符串而不是數組。 您需要像這樣explode()
字符串:
$result = empty($_POST['result']) ? array() : explode ( '|' ,$_POST['result']);
問題是您的input
標簽是一維的,因此您的內部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
標簽以生成2d數組:
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']
獲得一個二維數組,並能夠使用原始的嵌套循環迭代該數組
您可以使用此方法進行郵寄。
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'];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.