I need help splitting a long string into an array with specific index structure like this:
fname=bill&mname=&lname=jones&addr1=This%20House&...
I am looking to have the array set up as shown below:
myarray[0][0] = fname
myarray[0][1] = bill
myarray[1][0] = mname
myarray[1][1] =
myarray[2][0] = lname
myarray[2][1] = jones
myarray[3][0] = addr
myarray[3][1] = This House
The actual URL is longer than the example provided. So far, I have attempted to achieve this using the following code:
var
fArray = [],
nv = [],
myarray = [];
fArray = fields.split('&');
// split it into fArray[i]['name']="value"
for (i=0; i < fArray.length; i++) {
nv = fArray[i].split('=');
myarray.push(nv[0],nv[1]);
nv.length = 0;
}
The result should be stored in 'myarray', however, I am encountering an issue where I am getting a one-dimensional array instead of the desired two-dimensional array.
Subsequently, I plan to search for a specific key like 'lname' and retrieve its corresponding index. For instance, if it returns '3', I can then access the last name using myarray[3][1].
Do you think my approach makes sense or am I making it too complex?