Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
67 views
in Technique[技术] by (71.8m points)

javascript - String with brackets to Array PHP/JS

i have a Form in html with names like

"deliveryAddress[0][name]"
"deliveryAddress[0][firstName]"
"deliveryAddress[0][street]"

Now i have a JS function that makes a Json of this form:

var queryString = jq('#idOrderForm');
var returnArray = {};
for ( var i = 0; i < queryString[0].elements.length; i++ ) {
    var e = queryString[0].elements[i];
    returnArray[e.name] = e.value;
}
var json = JSON.stringify(returnArray);

The Result of this function is the following json:

"{"deliveryAddress[0][salutation]":"2","deliveryAddress[0][title]":"-1","deliveryAddress[0] [lastName]":"lastName","deliveryAddress[0][firstName]":"firstName","addrInput":"-1","deliveryAddress[0][country]":"D","deliveryAddress[0][zipCode]":"12345","deliveryAddress[0][city]":"City","deliveryAddress[0][street]":"Street","deliveryAddress[0][houseNo]":"1""save":"true"}"

I send this Json to a PHP Script that receive it and use json_decode. the result is the following:

json_decode($aParams['dataArray'], true, 512, JSON_THROW_ON_ERROR)


result = {array} [11]
deliveryAddress[0][salutation] = "2"
deliveryAddress[0][title] = "-1"
deliveryAddress[0][lastName] = "lastName"
deliveryAddress[0][firstName] = "firstName"
addrInput = "-1"
deliveryAddress[0][country] = "D"
deliveryAddress[0][zipCode] = "12345"
deliveryAddress[0][city] = "City"
deliveryAddress[0][street] = "Street"
deliveryAddress[0][houseNo] = "1"
save = "true"

What i need is a correct multidimensional array like:

result =  {array} [2]
 addrInput = "-1"
 deliveryAddress = {array} [1]
  0 = {array} [9]
   salutation = "2"
   title = "-1"
   lastName = "lastName"
   firstName = "firstName"
   country = "D"
   zipCode = "12345"
   city = "City"
   street = "Street"
   houseNo = "1"

I hope you understand my requirement and can help me :)

question from:https://stackoverflow.com/questions/65917352/string-with-brackets-to-array-php-js

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...