Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
186 views
in Technique[技术] by (71.8m points)

php - Wrong array needle used

I will try to describe my very strange problem with an multidimensional array.

This is my form (part of it)

<?php
    echo'
      <tr>
        <td>
            <div class="form-group" id="calc_norm_id_nr_div['.$y.'][]">
    
                    <select class="form-control no-border input-sm" id="calc_norm_id['.$y.'][]" name="calc_norm_id['.$y.'][]" multiple="multiple" size="12" style="overflow-y: auto;">';
    
                        $norm_id_arr = explode(',', $row_table_2['norm_id']);
    
                        foreach ($rows_nrm as $row_nrm)
                        {
                            echo '<option ';if(in_array($row_nrm['id'], $norm_id_arr)) { echo 'selected="selected"';} echo 'value="'.$row_nrm['id'].'">'.$row_nrm['norm'].' '.$row_nrm['omschrijving'].'</option>';
                        }
                        echo '
                    </select>
    
                <span class="" id="calc_norm_id_nr_glyp['.$y.'][]"></span>
            </div>
        </td>
        <td>
            <div class="form-group" id="calc_prijs_div['.$y.'][]">
                <input type="number" step="0.01" class="form-control no-border input-sm" id="calc_prijs['.$y.'][]" name="calc_prijs[]" value="'.$row_table_2['prijs'].'" style="text-align: right" onkeyup="validate(this, '.$y.')">
                <span class="" id="calc_prijs_glyp['.$y.'][]"></span>
            </div>
        </td>
      </tr>';
?>

The form is send without any issues and print_r($_POST) gives as expected:

$_POST Array ( [calc_norm_id] => Array ( [1] => Array ( [0] => 8 [1] => 3 [2] => 2 ) [2] => Array ( [0] => 1 [1] => 4 [2] => 5 ) ) [calc_prijs] => Array ( [0] => 454.00 [1] => 740.00 [2] => [3] => [4] => [5] => [6] => ) )

Other checks are print_r($_POST['calc_norm_id'] and print_r($_POST['calc_prijs']:

$_POST[calc_norm_id] Array ( [1] => Array ( [0] => 8 [1] => 3 [2] => 2 ) [2] => Array ( [0] => 1 [1] => 4 [2] => 5 ) )
$_POST[calc_prijs] Array ( [0] => 454.00 [1] => 740.00 [2] => [3] => [4] => [5] => [6] => )

Still going as expected.

Next step is looping through the Array to send to the database:

foreach($_POST['calc_prijs'] as $key=>$value)
{
$calc_prijs = trim(mysqli_real_escape_string($mysqli, $_POST['calc_prijs'][$key]));
if(is_array($_POST['calc_norm_id'][$key])) { $calc_norm_id = trim(mysqli_real_escape_string($mysqli, implode(',', $_POST['calc_norm_id'][$key]))); }
}

But when I check $calc_norm_id & $calc_prijs now I see that the content is not matching. I am expecting:

$calc_norm_id = 8,3,2
$calc_prijs = 454.00

$calc_norm_id = 1,4,5
$calc_prijs = 740.00

But I am getting:

$calc_norm_id = 
$calc_prijs = 454.00

$calc_norm_id = 8,3,2
$calc_prijs = 740.00

Any ideas of what happend here and how to solve this?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to align the subscripts in the array

$_POST[calc_norm_id] Array ( **[1]** => Array

TO

$_POST[calc_norm_id] Array ( **[0]** => Array

TO

$_POST[calc_norm_id] Array ( [0] => Array ( [0] => 8 [1] => 3 [2] => 2 ) [1] => Array ( [0] => 1 [1] => 4 [2] => 5 ) )
$_POST[calc_prijs] Array ( [0] => 454.00 [1] => 740.00 [2] => [3] => [4] => [5] => [6] => )

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...