Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在jqueryajaxdone函数问题中为数组赋值_Php_Javascript_Jquery_Magento - Fatal编程技术网

Php 在jqueryajaxdone函数问题中为数组赋值

Php 在jqueryajaxdone函数问题中为数组赋值,php,javascript,jquery,magento,Php,Javascript,Jquery,Magento,我试图将数据库中各个位置之间的距离添加到一个名为“距离”的数组中。我的数组似乎在完成循环之前被清空,并且/或者循环的索引没有在每次循环完成之前为“x”重新分配值。你能看出我做错了什么吗?我需要一个回调函数吗 <?php if (isset($customer_id)) {?> customer_id = <?=$customer_id?>; <?php }?>

我试图将数据库中各个位置之间的距离添加到一个名为“距离”的数组中。我的数组似乎在完成循环之前被清空,并且/或者循环的索引没有在每次循环完成之前为“x”重新分配值。你能看出我做错了什么吗?我需要一个回调函数吗

<?php if (isset($customer_id)) {?>
                        customer_id = <?=$customer_id?>;
                    <?php }?>

                    //var distances = new Array();

                        $j.ajax({
                            type: "POST",
                            url: "/ajax_calls/originDestinationAddress.php",
                            data: { 'vendorID': <?=$vendorId?>, 'customerID': customer_id}
                            }).done(function(data) {
                                var data= JSON.parse(data);     
                                var destination = data[0].destination;
                                console.log ('destination is ' + destination);

                                var distances = [];
                                for(var x=0; x<data.length; x++){
                                console.log('the array of distances for vendor with id ' + <?=$vendorId?>);
                                    var origin = data[x].origination;
                                    console.log('origin is ' + origin);

                                    if(origin && destination){
                                        var service = new google.maps.DistanceMatrixService();
                                          service.getDistanceMatrix(
                                            {
                                              origins: [origin],
                                              destinations: [destination],
                                              travelMode: google.maps.TravelMode.DRIVING,
                                              unitSystem: google.maps.UnitSystem.IMPERIAL,
                                              avoidHighways: false,
                                              avoidTolls: false
                                            }, function(response, status){
                                                if (status != google.maps.DistanceMatrixStatus.OK) {
                                                    console.log('Error was: ' + status);
                                                  } else {
                                                    var origins = response.originAddresses;
                                                    var destinations = response.destinationAddresses;

                                                    for (var i = 0; i < origins.length; i++) {
                                                      var results = response.rows[i].elements;
                                                      for (var j = 0; j < results.length; j++) {
                                                        var miles = results[j].distance.text;
                                                        var pieces = miles.split(" ");

                                                            //$j('#'+ id + ' td.distanceDetail').text(pieces[0]);
                                                            //$j('#'+ id + ' td.distanceDetail').append('<span> ' + pieces[1] + '</span>');
                                                            console.log('currently adding ' + pieces[0] + ' to distances array with key ' + x);
                                                            distances[x]= pieces[0];
                                                      }
                                                    }
                                                  }
                                            });
                                }//end for length of data loop
                                console.log(distances);
                            }


                        });//end done function

客户_id=;
//var距离=新数组();
$j.ajax({
类型:“POST”,
url:“/ajax\u calls/originDestinationAddress.php”,
数据:{'vendorID':,'customerID':客户id}
}).完成(功能(数据){
var data=JSON.parse(数据);
var destination=数据[0]。目标;
console.log('目的地是'+目的地);
var距离=[];

对于(var x=0;x哇,这很难理解

它看起来像您标记为的花括号:

//end for length of data loop
实际上是右大括号,用于:

if(origin && destination){
卷曲大括号直接位于以下位置之后:

console.log(distances);
是data.length循环的右大括号

我想您要做的是移动
console.log(距离)

在.done()回调结束前的一行,如下所示:

        }//end for length of data loop

    }
    console.log(distances);

});//end done function

很抱歉将代码粘贴到这里会弄乱格式!现在尝试您的建议。谢谢您的帮助!
        }//end for length of data loop

    }
    console.log(distances);

});//end done function