简体   繁体   中英

How to echo multidimentional array posted from php

I have 2 php files. 1st 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",";
       }
    }

I got the following from the previous 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,

Now, I want to post $result array to another php file. To post, 1st php contains:

foreach ($result as $key => $value) 
                {
                    foreach ($value as $k => $v) 
                    {
                        echo "<input type='hidden' name='result[$k]' value='$v'/>";
                    }
                }

To receive $result array, 2nd php:

$result = empty($_POST['result']) ? array() : $_POST['result'];
foreach ($result as $key => $value ) 
                {
                    foreach ($value as $k => $v) 
                    {
                        echo $v;
                    }
                }

But, the echo give me:

Invalid argument supplied for foreach().

It mean the second foreach()statement.

Any help please?

Edit 1: It is working now by fix 1st php as:

foreach ($result as $key => $value) 
{
    foreach ($value as $k => $v) 
    {
        echo "<input type='hidden' name='result[$key][$k]' value='$v'/>";
                                               ^^^^^^ add second key here
    }
}

Now, How can I save $result in the database? I want to:

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.

Now I try:

 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.

I believe I must include col2 (HitLink) in the insert tatement but what about $v?

$sql = "INSERT INTO BlastResultFact (Hit, HitLink) VALUES ('$v')";

The array you are getting from $_POST['result'] is a string not an array. You need to explode() the string like this:

$result = empty($_POST['result']) ? array() : explode ( '|' ,$_POST['result']);

The problem is that your input tags are 1 dimensional, so your inner foreach will fail because each element of $_POST['result'] is a string, not an array

$result = empty($_POST['result']) ? array() : $_POST['result'];
foreach ($result as $key => $value ) 
{
    // $value is a string!
    foreach ($value as $k => $v) 
    {
        echo $v;
    }
}

To correct the issue, modify your input tags to generate a 2d array:

foreach ($result as $key => $value) 
{
    foreach ($value as $k => $v) 
    {
        echo "<input type='hidden' name='result[$key][$k]' value='$v'/>";
                                               ^^^^^^ add second key here
    }
}

Now you should get a 2d array in $_POST['result'] and be able to iterate the array with your original nested loop

You Can POst using this Method.

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'];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM