将对象数组从javascript传递给php

时间:2015-01-21 00:41:19

标签: javascript php arrays

所以我构建了一个传递给PHP的对象数组,但是我想知道这是传递它们最干净的方法,并且在PHP中最容易处理。

基于我的谷歌地图反向地理编码器返回的内容,群组件的数量是可变的这一事实,我被部分抛弃了。

每个组都将作为一个单独的行插入到MySql中,其参数为' name'和'键入'

var neighborhood = extractLongFromAddress(results[0].address_components, "sublocality");
var town = extractLongFromAddress(results[0].address_components, "locality");
var stateShort = extractShortFromAddress(results[0].address_components, "administrative_area_level_1");
var stateLong = extractLongFromAddress(results[0].address_components, "administrative_area_level_1");
var country = extractLongFromAddress(results[0].address_components, "country");

var groups=[];
if(town && stateShort){
  groups.push({name: town+", "+stateShort,
           type:"city"
  });
}

if(neighborhood && stateLong){
  groups.push({name: neighborhood+", "+stateShort,
           type:"neighborhood"
  });
}

if(stateLong){
  groups.push({name:stateLong,
           type:"state"
  });
}

if(country){
  groups.push({name:country,
           type:"country"
  });
}
 console.log(groups);

衷心感谢任何帮助。非常感谢。

1 个答案:

答案 0 :(得分:-1)

只需将数组转换为JSON.stringify()的JSON字符串并将其发送到php;在PHP中,您将json_decode()