• <tfoot id='QeyWR'></tfoot>

    • <bdo id='QeyWR'></bdo><ul id='QeyWR'></ul>

      <small id='QeyWR'></small><noframes id='QeyWR'>

    1. <i id='QeyWR'><tr id='QeyWR'><dt id='QeyWR'><q id='QeyWR'><span id='QeyWR'><b id='QeyWR'><form id='QeyWR'><ins id='QeyWR'></ins><ul id='QeyWR'></ul><sub id='QeyWR'></sub></form><legend id='QeyWR'></legend><bdo id='QeyWR'><pre id='QeyWR'><center id='QeyWR'></center></pre></bdo></b><th id='QeyWR'></th></span></q></dt></tr></i><div id='QeyWR'><tfoot id='QeyWR'></tfoot><dl id='QeyWR'><fieldset id='QeyWR'></fieldset></dl></div>
      <legend id='QeyWR'><style id='QeyWR'><dir id='QeyWR'><q id='QeyWR'></q></dir></style></legend>
      1. php str_getcsv 数组问题

        时间:2023-12-01
            • <bdo id='RYLA1'></bdo><ul id='RYLA1'></ul>
                <tbody id='RYLA1'></tbody>
            • <small id='RYLA1'></small><noframes id='RYLA1'>

                <tfoot id='RYLA1'></tfoot>
                <i id='RYLA1'><tr id='RYLA1'><dt id='RYLA1'><q id='RYLA1'><span id='RYLA1'><b id='RYLA1'><form id='RYLA1'><ins id='RYLA1'></ins><ul id='RYLA1'></ul><sub id='RYLA1'></sub></form><legend id='RYLA1'></legend><bdo id='RYLA1'><pre id='RYLA1'><center id='RYLA1'></center></pre></bdo></b><th id='RYLA1'></th></span></q></dt></tr></i><div id='RYLA1'><tfoot id='RYLA1'></tfoot><dl id='RYLA1'><fieldset id='RYLA1'></fieldset></dl></div>
              1. <legend id='RYLA1'><style id='RYLA1'><dir id='RYLA1'><q id='RYLA1'></q></dir></style></legend>

                1. 本文介绍了php str_getcsv 数组问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在上传一个 csv 文件,然后使用 str_getcsv 对其进行解析.一切都很好,只是我需要一种方法来循环它们.理想情况下,让数组返回并看起来像这样会很棒:

                  I am uploading a csv file and then parsing it using str_getcsv. All works great except that I need a way to cycle through them. Ideally, it'd be great to have the array come back and look like this:

                  Array (      
                      [1] => Array
                         (
                              [0] => 1 // first id in csv
                              [1] => name
                              [2] => address
                              [3] => town
                              [4] => state
                              [5] => zip
                              [6] => phone
                              [7] => website
                              [8] => other
                              [9] => other
                          )
                      [22] => Array
                         (
                              [10] => name
                              [11] => address
                              [12] => town
                              [13] => state
                              [14] => zip
                              [15] => phone
                              [16] => website
                              [17] => other
                              [18] => other
                          )
                      [24] => Array
                         (
                              [19] => name
                              [20] => address
                              [21] => town
                              [22] => state
                              [23] => zip
                              [24] => phone
                              [25] => website
                              [26] => other
                              [27] => other
                          )
                  )
                  

                  但是数据返回如下:

                  Array
                  (
                      [0] => 1 // first id in csv
                      [1] => name
                      [2] => address
                      [3] => town
                      [4] => state
                      [5] => zip
                      [6] => phone
                      [7] => website
                      [8] => other
                      [9] => other
                  22 // id field
                      [10] => name
                      [11] => address
                      [12] => town
                      [13] => state
                      [14] => zip
                      [15] => phone
                      [16] => website
                      [17] => other
                      [18] => other
                  24// id field
                      [19] => name
                      [20] => address
                      [21] => town
                      [22] => state
                      [23] => zip
                      [24] => phone
                      [25] => website
                      [26] => other
                      [27] => other
                  

                  关于如何解决这个问题,让它看起来像顶部的数组有什么建议吗?现在我正在做:

                  Any suggestions on how to fix this to look like the array at the top? right now I'm doing:

                  $csvfile = file_get_contents($targetFile);
                  $csv = str_getcsv($csvfile);
                  

                  推荐答案

                  str_getcsv() 需要字符串作为参数传递为 一个 记录.
                  但是由于您的源无论如何都是一个文件,最简单的方法可能是使用 fgetcsv() 而不是 str_getcsv()

                  str_getcsv() expects the string passed as parameter to be one record.
                  But since your source is a file anyway the easiest way is probably to use fgetcsv() instead of str_getcsv()

                  $data = array();
                  $fp = fopen($targetFile, 'rb');
                  while(!feof($fp)) {
                      $data[] = fgetcsv($fp);
                  }
                  fclose($fp);
                  

                  <小时>

                  独立示例:


                  self-contained example:

                  <?php
                  $targetFile = 'soTest.csv';
                  setup($targetFile);
                  
                  $data = array();
                  $fp = fopen($targetFile, 'rb');
                  while(!feof($fp)) {
                      $data[] = fgetcsv($fp);
                  }
                  var_dump($data);
                  
                  
                  function setup($targetFile) {
                      file_put_contents($targetFile, <<< eot
                  1,name,address,town,state,zip,phone,website,other,other
                  2,name,address,town,state,zip,phone,website,other,other
                  3,name,address,town,state,zip,phone,website,other,other
                  eot
                      );
                  }
                  

                  打印

                  array(3) {
                    [0]=>
                    array(10) {
                      [0]=>
                      string(1) "1"
                      [1]=>
                      string(4) "name"
                      [2]=>
                      string(7) "address"
                      [3]=>
                      string(4) "town"
                      [4]=>
                      string(5) "state"
                      [5]=>
                      string(3) "zip"
                      [6]=>
                      string(5) "phone"
                      [7]=>
                      string(7) "website"
                      [8]=>
                      string(5) "other"
                      [9]=>
                      string(5) "other"
                    }
                    [1]=>
                    array(10) {
                      [0]=>
                      string(1) "2"
                      [1]=>
                      string(4) "name"
                      [2]=>
                      string(7) "address"
                      [3]=>
                      string(4) "town"
                      [4]=>
                      string(5) "state"
                      [5]=>
                      string(3) "zip"
                      [6]=>
                      string(5) "phone"
                      [7]=>
                      string(7) "website"
                      [8]=>
                      string(5) "other"
                      [9]=>
                      string(5) "other"
                    }
                    [2]=>
                    array(10) {
                      [0]=>
                      string(1) "3"
                      [1]=>
                      string(4) "name"
                      [2]=>
                      string(7) "address"
                      [3]=>
                      string(4) "town"
                      [4]=>
                      string(5) "state"
                      [5]=>
                      string(3) "zip"
                      [6]=>
                      string(5) "phone"
                      [7]=>
                      string(7) "website"
                      [8]=>
                      string(5) "other"
                      [9]=>
                      string(5) "other"
                    }
                  }
                  

                  <小时>

                  edit2:使用每条记录的第一个元素作为结果数组中的键:


                  edit2: For using the first element of each record as the key in the result array:

                  <?php
                  $targetFile = 'soTest.csv';
                  setup($targetFile);
                  
                  $data = array();
                  $fp = fopen($targetFile, 'rb');
                  while(!feof($fp)) {
                      $row = fgetcsv($fp);
                      $id = array_shift($row);
                      $data[$id] = $row;
                  }
                  var_dump($data);
                  
                  
                  function setup($targetFile) {
                      file_put_contents($targetFile, <<< eot
                  1,name,address,town,state,zip,phone,website,other,other
                  2,name,address,town,state,zip,phone,website,other,other
                  3,name,address,town,state,zip,phone,website,other,other
                  eot
                      );
                  }
                  

                  这篇关于php str_getcsv 数组问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:是否可以使用 HTML 表单/PHP 脚本上传文件夹 下一篇:创建自定义上传进度条

                  相关文章

                    <small id='aRPfr'></small><noframes id='aRPfr'>

                    <tfoot id='aRPfr'></tfoot>
                  1. <legend id='aRPfr'><style id='aRPfr'><dir id='aRPfr'><q id='aRPfr'></q></dir></style></legend>

                      <bdo id='aRPfr'></bdo><ul id='aRPfr'></ul>
                  2. <i id='aRPfr'><tr id='aRPfr'><dt id='aRPfr'><q id='aRPfr'><span id='aRPfr'><b id='aRPfr'><form id='aRPfr'><ins id='aRPfr'></ins><ul id='aRPfr'></ul><sub id='aRPfr'></sub></form><legend id='aRPfr'></legend><bdo id='aRPfr'><pre id='aRPfr'><center id='aRPfr'></center></pre></bdo></b><th id='aRPfr'></th></span></q></dt></tr></i><div id='aRPfr'><tfoot id='aRPfr'></tfoot><dl id='aRPfr'><fieldset id='aRPfr'></fieldset></dl></div>