[英]passing array of objects from javascript to php
因此,我構建了一個對象數組以傳遞給PHP,但是我想知道這是否是傳遞它們的最干凈方法,並且將是在PHP中最容易處理的方法。
基於我的Google Maps反向地理編碼器返回的內容,組組件的數量是可變的,這使我感到有些不滿意。
每個組將作為單獨的行插入到MySql中,並帶有'name'和'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);
真誠的感謝您的幫助。 非常感謝。
只需通過JSON.stringify()
將數組轉換為JSON字符串,然后將其發送到php; 在PHP中,您將執行json_decode()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.