【问题标题】:Passing array php into json gives undefined将数组 php 传递给 json 给出未定义
【发布时间】:2016-08-23 04:29:12
【问题描述】:

我想将一个 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] is undefined

我想在模态引导程序中将此数组显示到一个表中,所以我再次调试它以查看 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...}

所以,我像这样使用 for 循环:

 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 |
+---+-----------+

请指教。

【问题讨论】:

  • 好吧,我不确定你是否得到了正确的输出。但是如果您的 $tryone 数组不是关联数组,那么您不需要传递第二个参数 JSON_FORCE_OBJECT 并使用类似 $.each(response.isi, function(i, item) 的数组,如下面@Óscar Gómez Alcañiz 的回答所示.

标签: javascript php jquery arrays json


【解决方案1】:

在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']。不确定哪一个会根据您的数组类型起作用。

【讨论】:

    【解决方案2】:

    关于你更新后的错误:

    您在循环中访问了错误的索引。查看更正后的代码:

    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);
    });
    

    【讨论】:

      猜你喜欢
      • 2018-02-02
      • 2018-01-18
      • 1970-01-01
      • 2015-11-13
      • 1970-01-01
      • 2012-01-26
      • 2015-01-12
      • 2011-05-03
      • 1970-01-01
      相关资源
      最近更新 更多