drpph80800 2016-12-22 04:51
浏览 39
已采纳

用于修改值的循环数组(JavaScript到PHP并返回JavaScript)

I'm working with JavaScript and PHP using arrays, the first step is create the array, here

var ListaA=[];
var i = 0, len = options.length;
while (i < len){
            var tmp = {
                        'proyecto':'test',
                        'pendientes1':0,
                        'pendientes2':0,
                        'terminadas1':0,
                        'terminadas2':0,
                        'solucion':0
                };
                ListaA.push(tmp);
                i++;
            }

Then i send it to my PHP file like this

var laLista = JSON.stringify(ListaA);

$.get("php/operation.php?test="+ {'test' : laLista }, function( data ){
                var tmp = {
                        'proyecto':""+value['proyecto']+"",
                        'pendientes1':""+value['pendientes1']+"",
                        'pendientes2':""+value['pendientes2']+"",
                        'terminadas1':""+value['terminadas1']+"",
                        'terminadas2':""+value['terminadas2']+"",
                        'solucion':""+value['solucion']+""
                    };
                    ListaA.push(tmp);
            });

As you can see above i have ready the code to get the data which represents the array sent by the PHP file, so i got covered that part, my issue here is in my PHP file, here.

$arrayWork = json_decode($_POST['test']);

Then i want to loop, this time, just for testing i'm just taking one of the values and incresing it to watch the result, like this

foreach($arrayWork as $value){
     $value['pendientes1']++; // this is not working for me
}

I got the following: "invalid argument supplied in foreach". So, what's wrong with my code? and which is the properly way to loop it and return it to my JavaScript?

I hope you can help me out with this issue. Thank you for your time and attention, good night.

  • 写回答

2条回答 默认 最新

  • dream12001 2016-12-22 05:05
    关注

    Using this code

    $arrayWork = json_decode($_POST['test']);
    

    your json isn't really converted into an associated array, look at below

    mixed json_decode ( string $json [, bool $assoc = false [, int $depth = 512 [, int $options = 0 ]]] )

    assoc When TRUE, returned objects will be converted into associative arrays.

    To convert json object into an array just add true to the second parameter to it

    $arrayWork = json_decode($_POST['test'], true);**strong text**
    

    To increment an index value in an array

    foreach($arrayWork $key => as $value){
        $arrayWork['pendientes1']++;
    }
    

    Edited.

    also since you are using $_POST method change your ajax from $.get to $.post

    $.post("php/operation.php?test="+ {'test' : laLista }, function( data ){
          var result = JSON.parse(data); // parse json string into json object
           ...
        });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C