douchu2823 2017-03-30 06:38
浏览 13
已采纳

如何在ajax中拆分数组?

var rows_selected = [];
var rowId = data[0]; // conten [24,25,26,27 etc]
var namaFile = data[4]; // content [foto1.JPG, foto2.JPG, foto3.JPG, foto4.JPG ]

//If checkbox is checked and row ID is not in list of selected row IDs
rows_selected.push(rowId,namaFile);

var dataId=[];
$.each(rows_selected, function(index, rowId, namaFile){
  dataId.push(rowId,namaFile);
});

in ajax i sent dataId to controller

$.ajax({
   data: {"iddata": "[{"+dataId+"}]"},
});

but in controller iddata receive just one array like this 24,6a5236ee48848e648553a35d64c4f64dJPG,25,a31ea31db31483506fbd95e463d4c8ffJPG,26,042fd1eee499e5bb9c93853c0299b42eJPG,27,e243845bcc0447324d449e37b271c9ebJPG,28,8f331fa2a713f5268ab71698a65185b6JPG

some code in controller

$iddata=array();
    $iddata=array($_GET['iddata']);
    $a=explode(",", $iddata[0]);
    $b=preg_replace('/[^A-Za-z0-9\-]/', '', $a);
    $jumlahdata=count($b)/2;
    for($i=0;$i<$jumlahdata;$i++)
    {
        //delete on database
       $data=array('id_perjal'=>$b[$i]);
       $this->M_perjal_terpasang->Delete_Foto($data);
        //and i want deleting foto with unlink
       unlink("assets/images/uploads/"$b[$i]);
    }
  • 写回答

2条回答 默认 最新

  • dongtou8736 2017-03-30 08:42
    关注

    I would start by using a naming convention, and name arrays with plural words, so that you know that namaFiles (plural) is an array, and one element of it is called namaFile (singular). This will avoid some confusion.

    Let's say that two checkboxes are checked, and their IDs are different, then you would somehow add two rows to rows_selected. Currently, you push those values one after the other so that you get an array like [24, 'foto1.JPG', 27, 'foto4.JPG'], but you really want to get them grouped in pairs, like this:

    [{rowId: 24, namaFile: 'foto1.JPG'}, {rowId: 27, namaFile: 'foto4.JPG'}]
    

    So you should push the values like this (supposing you have an index):

    rows_selected.push( {rowId: rowIds[index], namaFile: namaFiles[index]} );
    

    This you would repeat for other pairs that need to be added.

    In your Ajax data, you should use a JSON string, like this:

    var ajaxobj = {
       data: {"iddata": JSON.stringify(rows_selected) },
    }
    

    On the server side you would convert the JSON back to an associative array, and then access the two elements of each pair by their key name:

    $iddata = $_GET['iddata'];
    $a = json_decode($iddata, true);
    foreach($a as $pair) {
       //delete in database
       $data = array('id_perjal' => $pair['rowId']);
       $this->M_perjal_terpasang->Delete_Foto($data);
       //delete foto with unlink
       unlink("assets/images/uploads/" . $pair['namaFile']);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失