I'm通過Ajax發送一個JavaScript對象與此structrucePHP json_decode無序陣列
[
{ name:'box', price:'20', id:'72', units : 2 },
{ name:'box2', price:'30', id:'73', units : 2 },
{ name:'box3', price:'40', id:'74', units : 2 }
]
獲取服務器上的數據這樣
$data = json_decode(file_get_contents("php://input"),true);
$queryconst = '';
foreach($data as $key => $value){
$format[$key] = $value;
$format_keys = array_keys($format[$key]);
$newArray = $this->slashesToArray($value);
$queryconst = $queryconst.'(\''.implode("','", $newArray).'\'),';
}
$queryconst = rtrim($queryconst, ",");
$query = "INSERT INTO format (".implode(",", $format_keys).") VALUES ".$queryconst;
如果I`M發送數據具有單個對象
[
{ name:'box', price:'20', id:'72', units : 2 }
]
一切工作正常
$query = INSERT INTO format (name,units,price,id) VALUES ('box','2','20','72')
的問題是當該數據具有多個對象
[
{ name:'box', price:'20', id:'72', units : 2 },
{ name:'box2', price:'30', id:'73', units : 2 },
{ name:'box3', price:'40', id:'74', units : 2 }
]
和查詢
$query = INSERT INTO format (price,name,units,product_id)
VALUES ('box','2','20','74'),('30','box2','2','74'),('40','box3','2','74')
FO所述第一對象的順序是從靜止不同和查詢失敗
任何線索?