繁体   English   中英

将数组php传递到json给出未定义

[英]Passing array php into json gives undefined

我想将JSON从php传递到js。

这是我的php文件:

 public function upload() {
    $record = 0;
    if (!empty($_FILES)) {
        $config['upload_path'] = "./assets/uploads";
        $config['allowed_types'] = 'csv';

        $this->load->library('upload', $config);
        if (!$this->upload->do_upload("file")) {
            echo "File cannot be uploaded";
        } else {
            $upload_data = $this->upload->data();
            $csv = $upload_data['full_path'];
            $name_csv = $upload_data['file_name'];
            $tryOne = array();

            if (file_exists($csv)) {
                $file = fopen($csv, 'r'); // r flag is for readonly mode

                while (( $line = fgetcsv($file) ) !== false) { // if line exists
                    $tryOne[] = $line; // add to array
                }

                fclose($file);
            }

            echo json_encode(array(
                "nama_csv" => $name_csv,
                "path" => $csv,
                "isi" => $tryOne
            ));
        }
    } elseif ($this->input->post('file_to_remove')) {
        $file_to_remove = $this->input->post('file_to_remove');
        unlink("./assets/uploads/" . $file_to_remove);
    } else {
        $this->listFiles();
    }
}

请参阅mv json。 我调试它: print_r($tryOne);

Array
(
[0] => Array
    (
        [0] => NO
        [1] => COLUMN1
        [2] => COLUMN2
        [3] => COLUMN3
        [4] => COLUMN4
        [5] => COLUMN5
        [6] => COLUMN6
        [7] => COLUMN7
        [8] => COLUMN8
        [9] => COULMN9
        [10] => COLUMN10
        [11] => COLUMN11
        [12] => COLUMN12
        [13] => COLUMN13
    )

[1] => Array
    (
        [0] => 1
        [1] => NYK FUJI                      
        [2] => AJU150708 
        [3] =>                     
        [4] => 6C7132    
        [5] => 977NEF    
        [6] => JKT-P.T.IRON WORKS            
        [7] => 977NEF    
        [8] => KCH8ATDM  
        [9] => 17.9
        [10] => 1690
        [11] => 2150
        [12] => 6C7132-1690         
        [13] => 175
    )

[2] => Array
    (
        [0] => 2
        [1] => NYK FUJI                      
        [2] => AJU150708 
        [3] =>                     
        [4] => 6C7132    
        [5] => 977NEF    
        [6] => JKT-P.T.IRON WORKS            
        [7] => 977NEF    
        [8] => KCH8ATDM  
        [9] => 17.9
        [10] => 1700
        [11] => 2138
        [12] => 6C7132-1700         
        [13] => 176
    )

)

一切看起来都很好。 但是在ajax成功中:

 success: function (response) {
                    moment.locale("id");
                    for (i = 0; i <= response.isi.length; i++) {
                        for (j = 0; j <= response.isi[i].length; j++) {
                            $('#table-review').find('tbody').append("<tr>" +
                                    "<td>" + response.isi[i][j] + "</td>" +
                                    "</tr>");
                        }
                    }
                    $("#btnSave").attr("onclick", "save('" + response.path + "', '" + response.nama_csv + "')");

                    $('#modal_form').modal('show'); // show bootstrap modal
                    $('.modal-title').text('Review Data Hasil Upload Dari CSV Pusat'); // Set Title to Bootstrap modal title


                    reload_table();
                    listFilesOnServer();
                },

给我: TypeError:response.isi [i]未定义

我想将此数组显示为模式引导程序中的表,因此我再次对其进行调试以查看response.isi是否存在: console.log(response.isi)

 [["NO", "COLUMN1", "COLUMN2", 11 more...]

我有点困惑,为什么在php数组和js数组中看起来不同,为什么在php中将其分隔为元素,但是在js中将其以逗号分隔。

更新

根据下面的建议,我得到了这样的回调:

 Object { 0="NO",  1="COLUMN1",  2="COLUMN2",  more...}
 Object { 0="1",  1="NYK FUJI",  2="AJU150708 ",  more...}
 Object { 0="2",  1="NYK FUJI",  2="AJU150708 ",  more...}

所以,我使用这样的循环:

 index = 1;
  $.each(response.isi, function (i, item) {
  $('#table-review').find('tbody').append("<tr>" +
      "<td>" + index + "</td>" +
      "<td>" + item.i + "</td>" +
      "</tr>");
    index++;
    console.log(item);
  });

它给了我:

+---+-----------+
| 1 | undefined |
| 2 | undefined |
| 3 | undefined |
+---+-----------+

请指教。

在PHP中像这样编码

echo json_encode(array(
            "nama_csv" => $name_csv,
            "path" => $csv,
            "isi" => $tryOne
        ), JSON_FORCE_OBJECT);

在ajax中,您必须解析它。 正如您似乎正在使用jquery一样,您可以这样做

data = jQuery.parseJSON(response);

然后你得到数组。 使用像data.isidata['isi'] data.isi data['isi'] 不知道哪一种可以根据您的阵列类型工作。

对于更新后的错误:

您正在循环中访问错误的索引。 请参阅更正的代码:

var index = 1;
$.each(response.isi, function (i, item) {
$('#table-review').find('tbody').append("<tr>" +
    "<td>" + index + "</td>" +
    //"<td>" + item.i + "</td>" + // wrong: i doesn't exist as a property of item
    "<td>" + item[i] + "</td>" + // right: item array at index i will work
    "</tr>");
  index++;
  console.log(item);
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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