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 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛