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 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?