简体   繁体   中英

passing array of objects from javascript to php

So I built an array of objects for passing to PHP, but I am wondering wether this is the cleanest way to pass them and will be the easiest to deal with in PHP.

I am partly thrown off by the fact that the number of group components are variable based on what my google maps reverse-geocoder returns.

Each group will be inserted as a separate row into MySql with the parameters of 'name' and 'type'

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);

sincere thanks for any help. It is greatly appreciated.

Just convert the array to a JSON string by JSON.stringify() and send it to php; In PHP you'll do json_decode()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM