duanbu1998 2018-09-09 15:43
浏览 44
已采纳

使用多维数组并在新结构中使用它们

I have this form that is populated by a CSV input and the user needs to assign an header to the columns he would like to use. The user can also disable the rows he does not want to process.

This is my CSV

Klant;Klantref;Posnr.;Aantal;Dikte;Kwaliteit
Client;;60-017-301;8;10;S355
Client;;60-117-301;4;10;S355
Client;;60-031-302;2;20;S355
Client;;60-131-401;4;15;S355

This is the form:

$check_upload = glob('uploads/temp_dropzone/'.$_SESSION['user_id'].'_'.$upload_key.'_*.*');

echo'
<table class="table">
    <thead>';

    //head
    foreach($check_upload as $val)
    {
        $file = fopen($val,'r');

        $data_1 = fgetcsv($file);

        echo'<tr>';

        foreach ($data_1 as $index => $val_1)
        {
            $data_2 = explode(';', $val_1);

                echo '<th><input type="text" class="form-control no-border input-sm" id="calc_import['.$y.'][]" name="calc_import[]" value="Import: ja - nee" style="text-align: center" readonly="readonly"></th>';

            for($y = 1; $y <= count($data_2); $y++)
            {
                echo '<th>
                      <select class="form-control no-border input-sm" id="calc_header['.$y.'][]" name="calc_header[]">
                        <option value="">Kies type waarde</option>
                        <option value="aantal">Aantal</option>
                        <option value="omschr_1">Omschrijving 1</option>
                        <option value="omschr_2">Omschrijving 2</option>
                        <option value="omschr_3">Omschrijving 3</option>
                        <option value="prijs">Prijs</option>
                        <option value="opmerking">Opmerking</option>
                      </select>
                  </th>';
            }
        }
        echo'</tr>';

        fclose($file);
    }
    echo'
    </thead>
    <tbody>';

    // body
    foreach($check_upload as $val)
    {
        $file = fopen($val,'r');

        while($data_1 = fgetcsv($file))
        {
            $z++;

            echo'<tr>';

            foreach ($data_1 as $index => $val_1)
            {
                $data_2 = explode(';', $val_1);

                echo '<td align="center"><input type="radio" id="calc_import['.$z.'][]" name="calc_import['.$z.'][]" value="ja" checked> <input type="radio" id="calc_import['.$z.'][]" name="calc_import['.$z.'][]" value="nee"></td>';

                foreach ($data_2 as $index => $val_2)
                {
                    echo '<td><input type="text" class="form-control no-border input-sm" id="calc_body['.$z.'][]" name="calc_body['.$z.'][]" value="'.$val_2.'" readonly="readonly"></td>';
                }
            }

            echo '</tr>';
        }

        fclose($file);      
        unlink($val);
    }

    echo'
  </tbody>
</table>

The form is filled like this:

So far so good :)

When I send the form with $_POST and print_r($_POST); This is the create array

Array ( [dossier_id] => 9111 [calc_import] => Array ( [0] => Import: ja - nee [1] => Array ( [0] => nee ) [2] => Array ( [0] => ja ) [3] => Array ( [0] => ja ) [4] => Array ( [0] => ja ) [5] => Array ( [0] => ja ) ) [calc_header] => Array ( [0] => [1] => [2] => omschr_1 [3] => aantal [4] => omschr_2 [5] => omschr_3 ) [calc_body] => Array ( [1] => Array ( [0] => Klant [1] => Klantreferentie [2] => Posnr. [3] => Aantal [4] => Dikte [5] => Kwaliteit ) [2] => Array ( [0] => Client [1] => [2] => 60-017-301 [3] => 8 [4] => 10 [5] => S355 ) [3] => Array ( [0] => Client [1] => [2] => 60-117-301 [3] => 4 [4] => 10 [5] => S355 ) [4] => Array ( [0] => Client [1] => [2] => 60-031-302 [3] => 2 [4] => 20 [5] => S355 ) [5] => Array ( [0] => Client [1] => [2] => 60-131-401 [3] => 4 [4] => 15 [5] => S355 ) ) ) 

Now need to create an table with where I have an column with 'Aantal', Omschrijving etc. So I started to find the keys of them in the array with:

echo 'Aantal: '.array_search('aantal', $_POST['calc_header']);
echo '<br>';
echo 'Omschr_1: '.array_search('omschr_1', $_POST['calc_header']);
echo '<br>';
echo 'Omschr_2: '.array_search('omschr_2', $_POST['calc_header']);
echo '<br>';
echo 'Omschr_3: '.array_search('omschr_3', $_POST['calc_header']);
echo '<br>';
echo 'Prijs: '.array_search('prijs', $_POST['calc_header']);
echo '<br>';
echo 'Opmerking: '.array_search('opmerking', $_POST['calc_header']);

But what now? I want all variables of the rows when import is ja (yes) to be processed as

But I am getting stuck now, seen to much code, and I am don;t know for sure if the array has the right structure.

Please help, is the array structure right and how to go from here?

  • 写回答

2条回答 默认 最新

  • dragon321723 2018-09-09 16:46
    关注

    This code should help, I've included comments in the code to try and indicate what each item does...

    $_POST["calc_import"] = Array ( 0 => "Import: ja - nee",
             1 => Array ( "0" => "nee" ),
             2 => Array ( "0" => "ja" ),
             3 => Array ( "0" => "ja" ),
             4 => Array ( "0" => "ja" ),
             5 => Array ( "0" => "ja" ) );
    $_POST["calc_header"] = Array ( 0 => "",
            1 => "",
            2 => "omschr_1",
            3 => "aantal",
            4 => "omschr_2",
            5 => "omschr_3" );
    
    // This is where the code decides the positions of each column 
    // (you already do this - just store this for the processing)
    $columnPositions = [3, 2, 4, 5, "", ""]; 
    $fileName = "a.txt";
    $import = fopen($fileName, "r");
    // rowNumber is the match to check if this row is output
    $rowNumber = 1;
    $output = [];
    while ( $rowData = fgetcsv($import, null, ";")) {
        // Is this row to be exported?
        if ( $_POST["calc_import"][$rowNumber][0] == "ja" )    {
            $newOut = [];
            // Build up the output from the column positions
            foreach ( $columnPositions as $extractColumn )  {
                // Only output columns which have a position (i.e. non blank)
                if ( $extractColumn )  {
                    $newOut[] = $rowData[$extractColumn];
                }
            }
            $output[] = $newOut;
        }
        // Increment row number
        $rowNumber++;
    }
    fclose($import);     
    print_r($output);
    

    The basis is to take the column ordering you already have and turn it into a pick list of the data columns to extract. Each time you read a row from the file, first check if it is to be processed and then for each row re-order the data (throwing away unwanted data) and accumulate an output of the combined data.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi